清除setTimeout
我对 javascript/jquery 不太感兴趣,但我想做的只是向 mouseenter
函数添加一个超时,我不能做任何戏剧,但我也想清除超时如果用户在超时到期之前离开该元素 - 主要是为了允许光标跳过触发元素。
代码如下(mouseenter 有效,mouseleave 有效,但不清除超时):
$(document).ready(function() {
var timeout;
$('#mainMenu ul li').mouseenter(function() {
var dropTab = 'ul.' + this.id + 'Dropdown';
timeout = setTimeout( function(){
$(dropTab).slideToggle("fast") }, 500
);
});
$('#mainMenu ul li').mouseleave(function() {
clearTimeout(timeout);
var dropTab = 'ul.' + this.id + 'Dropdown';
setTimeout( function(){
$(dropTab).slideToggle("fast") }, 250
);
});
});
I'm not big on javascript/jquery, but what I'm trying to do is simply add a timout to a mouseenter
function, which I can do no dramas, but I also want to clear the timeout if the user leaves the element before the timeout expires - mainly to allow for cursor skipping over the triggering element.
Code is below (mouseenter works, mouseleave works but doesn't clear the timeout):
$(document).ready(function() {
var timeout;
$('#mainMenu ul li').mouseenter(function() {
var dropTab = 'ul.' + this.id + 'Dropdown';
timeout = setTimeout( function(){
$(dropTab).slideToggle("fast") }, 500
);
});
$('#mainMenu ul li').mouseleave(function() {
clearTimeout(timeout);
var dropTab = 'ul.' + this.id + 'Dropdown';
setTimeout( function(){
$(dropTab).slideToggle("fast") }, 250
);
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
也许您可以尝试使用
.hover()
。这个例子对我有用: http://jsbin.com/egaho3/editPerhaps you can try using
.hover()
. This example works for me: http://jsbin.com/egaho3/edit当然,缓动会增加用户体验。在不过度编程的情况下,我会
clearTimeout( )
像这样:(jsFiddle)Easing would add to the UX, for sure. Without being overly-programmatic, I'd
clearTimeout( )
like this: (jsFiddle)这可能可以更多地解释问题: JQuery,setTimeout 不起作用
This might be able to explain the problem more: JQuery, setTimeout not working
试试这个..我不确定。
$(文档).ready(function() {
});
try this ..i'm not sure .
$(document).ready(function() {
});
尝试 .hover(),可能会起作用:
Try .hover(), might work :