关于clearTimeout的疑问

<div>
<p>1This is a paragraph.</p>
<p>2This is another paragraph.</p>
<p>3This is another paragraph.</p>
<p>4This is another paragraph.</p>
</div>
$(function(){
function xx(){
    $("p").eq(0).animate({marginLeft:-100},1000,function(){$(this).detach().appendTo("div").css("margin-left","0px");timeout=setTimeout(xx,1000);})    
    }
xx();//执行xx

$("div").click(function(){
                clearTimeout(timeout);//点击过快导致cleartimeout多次合并成一次执行
                var xaa=Math.random();//随便执行一条语句
                
                setTimeout(xx,2000);//2000后继续执行xx

                })
})

问题是:xx是一个循环的函数,现在希望鼠标单击div后循环停止,执行一条语句,然后2000ms后继续执行xx,如何实现??
上面的click代码不正确,当点击过快时,导致并未有效清除定时器,求指点

阅读 1.9k
1 个回答

你确定代码不会报错?timeout都没声明吧。

function xx(){
    alert(1)
}
xx();//执行xx
var timeout ;
$("div").click(function(){
    clearTimeout(timeout);//点击过快导致cleartimeout多次合并成一次执行
    timeout = setTimeout(xx,2000);/
})
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题