2009-09-26 66 views
2

我目前有一个jQuery.event侦听器,一旦触发器将显示一个隐藏的元素(基本上是滚动)。在事件发生后等待x毫秒,重新检查并触发

但是,有没有一种方法可以让jQuery等待几毫秒,重新检查以确保鼠标仍然在该元素上,然后触发.show()事件(如果它是?

我目前有:

$("#who-mousearea").mouseenter(function(){ 
    $("a#who-edit").fadeIn(); 
}).mouseleave(function(){ 
    $("a#who-edit").fadeOut(); 
}); 

我明白我可以使用setTimeout的,然而这也只是延缓它需要淡入时间()的元素。

任何人都有如何实现这个想法?

回答

5
var timeout = null; 

$("#who-mousearea").mouseenter(function(){ 
    timeout = setTimeout(function() { 
    timeout = null; 
    $("a#who-edit").fadeIn(); 
    }, 50); 
}).mouseleave(function(){ 
    if (timeout == null) { 
    $("a#who-edit").fadeOut(); 
    } 
    else { 
    clearTimeout(timeout); 
    } 
}); 
+0

完美,谢谢约翰。 – jakeisonline 2009-09-26 19:59:03

0

在鼠标离开时清除超时。