2011-07-13 17 views
0

我正在实现一个简单的图像交换功能,并在jQuery中淡入淡出。停止触发下一个mouseover事件,直到内部函数完成

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

如果我真的很快地移动鼠标,mouseover会持续发射,交换图像的代码不能跟上。如何停止$('#thumbs img')上的mouseover事件,直到内部代码执行?

回答

0

可以删除事件处理常式,而处理:

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

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

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

+0

递归调用'doStuff'我认为这将无限循环 – Rafay

+0

我不认为它不使用得当的话,因为我已经做到了这一点之前。它只在事件是triggeres时才调用dostuff,而不是在dostuff本身中调用,所以它不是递归的。 –

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; 
    } 
}); 
相关问题