在事件发生后等待x毫秒,重新检查并触发

jak*_*ine 2 javascript jquery dom

我目前有一个jQuery.event监听器,一旦触发器将显示一个隐藏的元素(基本上是翻转).

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

我目前有:

$("#who-mousearea").mouseenter(function(){  
    $("a#who-edit").fadeIn();  
}).mouseleave(function(){  
    $("a#who-edit").fadeOut();  
});
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用setTimeout,但这只会延迟fadeIn()元素所需的时间.

任何人都知道如何实现这一目标?

Joh*_*ica 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);
  }
});
Run Code Online (Sandbox Code Playgroud)