停止下一次鼠标悬停事件,直到内部功能完成

时间:2011-07-13 08:45:06

标签: javascript jquery

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

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

如果我非常快速地移动鼠标,鼠标悬停会继续发射,交换图像的代码也无法跟上。如何停止$('#thumbs img')上的mouseover事件,直到内部代码执行为止?

2 个答案:

答案 0 :(得分:4)

$('#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;
  }
});

答案 1 :(得分:0)

您可以在处理时删除evant处理程序:

$('#thumbs img').mouseover(dostuff);

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

(类似的东西 - 我无法测试它)。你正在做的是在处理时删除事件处理程序,并在事件处理程序完成后重新绑定。

相关问题