如何使用 javascript/jquery 禁用鼠标悬停事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11295221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:40:43  来源:igfitidea点击:

How to disable mouseover event with javascript/jquery?

javascriptjqueryevents

提问by Jasminder Pal Singh

In my code a function execute when user mouseover on a div for first time and the function take like 30 seconds to complete, during that 30 seconds if user mouseover the same div again , the function executes again so i want to disable mouseover event for the second time. Is there any javascript or jquery code to disable the even from that div? THanks , any help will be heartly appreciated.

在我的代码中,当用户第一次将鼠标悬停在 div 上时执行一个函数,该函数需要 30 秒才能完成,在这 30 秒内,如果用户再次将鼠标悬停在同一个 div 上,该函数将再次执行,因此我想禁用鼠标悬停事件第二次。是否有任何 javascript 或 jquery 代码可以禁用该 div 的偶数?谢谢,任何帮助将不胜感激。

回答by adeneo

Use jQuery's on() and off() with some sort of callback, for example :

将 jQuery 的 on() 和 off() 与某种回调一起使用,例如:

$("#myElementID").on('mouseover', myFunction);

myFunction(e) {
    var myElement = e.target;
    myElement.off('mouseover', myFunction);
    //do something that takes 30 seconds
    myElement.animate({top: 1000}, 30000, function() { //callback
        myElement.on('mouseover', myFunction);
    });
}

回答by nemo

var isExecuting = false;

function yourThirtySecFunction() {
  if (isExecuting == true) return;
  isExecuting = true;
  //do your stuff here

  isExecuting = false;
}

回答by Sujit Agarwal

You can have a look at an interesting feature of jquery ... .one()here is the linkfor it

你可以看看 jquery 的一个有趣的特性…….one()这是它的链接

Description: Attach a handler to an event for the elements. The handler is executed at most once per element.

描述:将处理程序附加到元素的事件。每个元素最多执行一次处理程序。

2nd idea

第二个想法

I can give you one more idea, suppose when the mouse over is occurred once, just unbind the mouseover event before starting your operations, then as soon as the operations are over, bind them back again...

我可以再给你一个想法,假设当鼠标悬停发生一次时,只需在开始操作之前解除鼠标悬停事件的绑定,然后一旦操作结束,再次绑定它们......