javascript - 关于clearTimeout的疑问
巴扎黑
巴扎黑 2017-04-11 08:59:56
[JavaScript讨论组]
<p>
<p>1This is a paragraph.</p>
<p>2This is another paragraph.</p>
<p>3This is another paragraph.</p>
<p>4This is another paragraph.</p>
</p>
$(function(){
function xx(){
    $("p").eq(0).animate({marginLeft:-100},1000,function(){$(this).detach().appendTo("p").css("margin-left","0px");timeout=setTimeout(xx,1000);})    
    }
xx();//执行xx

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

                })
})

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

巴扎黑
巴扎黑

全部回复(1)
大家讲道理

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

function xx(){
    alert(1)
}
xx();//执行xx
var timeout ;
$("p").click(function(){
    clearTimeout(timeout);//点击过快导致cleartimeout多次合并成一次执行
    timeout = setTimeout(xx,2000);/
})
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号