停止下一个鼠标悬停事件的触发,直到内部函数完成

Stop next mouseover event from firing until inner function has finished

本文关键字:内部函数 下一个 鼠标 悬停 事件      更新时间:2023-09-26

我正在用jquery实现一个简单的图像交换功能。

$('#thumbs img').mouseover(function(){  
// code to swap images here
});

如果我移动鼠标非常快,鼠标上方一直触发和代码交换图像不能跟上。如何在$('#thumbs img')上停止鼠标悬停事件,直到内部代码执行?

$('#thumbs img').mouseover(function(){
  if(!moving){
    moving = true;
    // code to swap images here
    if(complete) // some test to see if image swap has finished
      moving = false;
  }
});

可以在处理过程中删除事件处理程序:

$('#thumbs img').mouseover(dostuff);
function dostuff()
{
    $(this).unbind('mouseover', dostuff);
    // code to swap images here
    $(this).mouseover(dostuff);
}

(类似的东西-我不能测试出来)。您所做的是在处理事件处理程序时删除它,并在它完成后重新绑定。