过一段时间后观察mouseenter事件
Observe mouseenter event after sometime
我有一个jquery代码,用于观察站点菜单栏上的mouseenter
事件。
menubar.on('mouseenter', 'li.parent.level0', function() {
... function body
});
现在我想提供一个延迟,以便事件函数体在2000 ms
之后执行,如下所示:
menubar.on('mouseenter', 'li.parent.level0', function() {
delay(2000);
... function body
});
我尝试了以下方法:
menubar.on('mouseenter', 'li.parent.level0', function() {
delay(2000);
... function body
});
var delay = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
但它仍然没有考虑延迟,只是在mouseenter
上立即执行菜单代码。
如何做到这一点?
您似乎在寻找一个常规的setTimeout
,它会延迟您放入其中的任何代码的执行。
menubar.on('mouseenter', 'li.parent.level0', function() {
setTimeout(function() {
// ... function body
},2000);
});
只需使用setTimeout
:
menubar.on('mouseenter', 'li.parent.level0', function() {
setTimeout(function() {
// Do the work here
}, 2000);
});
如果他们在元素激发之前离开,你可能想取消:
var menubarMouseEnterTimerHandle = 0;
menubar
.on('mouseenter', 'li.parent.level0', function() {
clearTimeout(menubarMouseEnterTimerHandle); // Just in case, but we shouldn't get repeated mouseenters...
menubarMouseEnterTimerHandle = setTimeout(function() {
// Do the work here
}, 2000);
})
.on('mouseleave', 'li.parent.level0', function() {
clearTimeout(menubarMouseEnterTimerHandle);
menubarMouseEnterTimerHandle = 0;
});
请注意,0
是一个无效的定时器句柄,如果句柄是0
,clearTimeout
将忽略对它的调用,因此我们不需要保护对上面clearTimeout
的调用:
相关文章:
- 使用jQuery删除动态创建的元素上的mouseenter事件
- IE 11未在SVG地图上启动Mouseenter或Mouseleave事件
- jQuery Mouseenter事件没有启动,它在单击时启动
- WordPress中的jQuery-在mouseenter和mouseleave事件上进行字体反转
- 如何区分mouseenter和tap事件
- 移动设备中使用mouseenter和mouseleave事件的错误
- 拖动html元素时检测到mouseenter事件
- 如何告诉Jquery只运行最后一个mouseenter事件
- 为什么事件mouseenter在JavaScript中触发两次
- Angularjs:防止对mouseenter/mouseleave事件的脏检查
- MouseEnter 事件失火,即使在 AngularJS 中也有延迟
- 如何将 mouseenter 事件侦听器附加到 sap.m.StandardListItem
- Jquery/CSS:如何在mouseenter事件上应用延迟1秒的addClass
- 过一段时间后观察mouseenter事件
- Javascript mouseenter事件激发了两次
- JavaScript将mouseenter事件传递给位于另一个元素下面的元素
- GMaps v3: addDomListener mouseenter事件处理不工作在Chrome, Safari
- jQuery mouseenter事件未执行
- jQuery mouseenter事件无法控制地触发
- jQuery委托mouseenter事件只在点击时触发