在下面的代码片段中,只有在一段时间内连续停留在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>发布于 2011-11-02 20:14:30
您需要为每个div添加一个额外的闭包,以便tt变量是唯一的。使用.each绑定事件侦听器,有效地为每个tt变量创建一个新的闭包:
obj.find('div').each(function(){
var tt;
var that = $(this)
that.bind({
mouseenter:function(e){
clearTimeout(tt); // In case something weird happens
tt = setTimeout(function(){
change(e,that)
},1000) // <-- time to wait before execution
},
mouseleave:function(e){
clearTimeout(tt);
}
});
});发布于 2011-11-02 20:20:56
尝试使用增加延迟http://cherne.net/brian/resources/jquery.hoverIntent.html的悬停意图
发布于 2011-11-02 20:05:10
您需要在HTML中的某个位置包含id ='aaa‘的div
<div id='aaa'><!--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-->在这里查看jsfiddle http://jsfiddle.net/vR5hJ/
https://stackoverflow.com/questions/7979840
复制相似问题