jQuery-Javascript setTimeout-clearTimeout 不起作用
在下面的代码片段中,只有当我在 div
上连续停留一段时间时,我才会调用 change()
函数。如果我只是浏览 div
,我想使用 clearTimeout
取消对该函数的调用。
我发现clearTimeout 不起作用。请有人帮助我。提前致谢。
jQuery 部分:
var obj=$('#aaa');
var tt;
obj.find('div').bind({
mouseenter:function(e){
var that = $(this)
tt = setTimeout(function(){
change(e,that)
},1000) // <-- time to wait before execution
},
mouseleave:function(e){
clearTimeout(tt);
}
});
function change(e,that){
console.log(e)
console.log(that)
}
HTML 部分:
<div id='aaa'>
<div><!--class a start-->
<div>lkaiseulaweg</div>
<div><!--class b start-->
<div>ae</div>
<div>dd</div>
</div><!--class b end-->
</div><!--class a end-->
<div><!--class a start-->
<i>numbers</i>
<div><!--class b start-->
<div>986</div>
<div>345</div>
<div>000</div>
<div>999</div>
</div><!--class b end-->
</div><!--class a end-->
</div>
In the snippet below, I would like to call the change()
function only if I have stayed continuously over a div
for a period of time. If I just skim over the div
, I would like to cancel out the call to the function - using clearTimeout
.
I see that the clearTimeout is not working. Someone please help me. Thanks in advance.
jQuery portion :
var obj=$('#aaa');
var tt;
obj.find('div').bind({
mouseenter:function(e){
var that = $(this)
tt = setTimeout(function(){
change(e,that)
},1000) // <-- time to wait before execution
},
mouseleave:function(e){
clearTimeout(tt);
}
});
function change(e,that){
console.log(e)
console.log(that)
}
HTML portion :
<div id='aaa'>
<div><!--class a start-->
<div>lkaiseulaweg</div>
<div><!--class b start-->
<div>ae</div>
<div>dd</div>
</div><!--class b end-->
</div><!--class a end-->
<div><!--class a start-->
<i>numbers</i>
<div><!--class b start-->
<div>986</div>
<div>345</div>
<div>000</div>
<div>999</div>
</div><!--class b end-->
</div><!--class a end-->
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
每个 div 都需要一个额外的闭包,以便 tt 变量是唯一的。使用 .each 绑定事件监听器,有效地为每个 tt 变量创建一个新的闭包:
You need an extra closure for each div, so that the
tt
variable is unique. Use.each
to bind event listeners, effectively creating a new closure for eachtt
variable:尝试悬停意图,这会增加延迟 http://cherne.net/brian/resources/jquery.hoverIntent .html
Try hover intent which adds delays http://cherne.net/brian/resources/jquery.hoverIntent.html
你需要有一个 id ='aaa' 的 div 在你的 HTML 中的某个地方
看到 jsfiddle http://jsfiddle.net /vR5hJ/
you need to have a div with id ='aaa' some where in your HTML
see the jsfiddle here http://jsfiddle.net/vR5hJ/
它不起作用,因为您设置了许多超时并将它们存储在同一个变量中,因此您设置的第一个超时将被下一个设置的超时覆盖,依此类推。您可以通过删除
#aaa
内除一个 div 之外的所有 div 来检查这一点,看看它是否工作正常。您需要做的是在设置之前清除旧的超时:
It doesn't work because you set many timeouts and store them in the same variable so the first one you set will be overwritten by the next and so on. You can check this by removing all divs inside
#aaa
except one and see that it works fine.What you need to do is clear the old timeout before you set it:
第一篇文章:你错过了一个“;”在
var that = $(this)
好,似乎不是问题...(但是 jsfiddle 不验证它!)
但是对于 jsfiddle,您需要定义
change
函数之前使用它。http://jsfiddle.net/bouillard/RTSFR/
First post: you miss a ";" after
var that = $(this)
OK, seems not a problem... (but jsfiddle don't valid it !)
But for jsfiddle, you need to define
change
function before to use it.http://jsfiddle.net/bouillard/RTSFR/