clearTimeout怎么用

托泥羊 2014-09-14 11:03:56
下面这个函数怎么结束掉setTimeout??

function moveToTop(classNa,distenceInit,distence,speed)
{
console.debug("start......");
element=document.getElementsByClassName(classNa)[0];
var topTemp=parseInt(element.style.top);
console.debug("topTemp:"+topTemp);

if (topTemp <= (distenceInit - distence)) {
clearTimeout(t); //——这个怎么不起作用啊???
console.debug("end if,truely end......");
} else {
topTemp--;
element.style.top = topTemp + "px";
}
var repeat = "moveToTop('" + classNa + "'," + distenceInit + "," + distence + "," + speed + ")";
t = setTimeout(repeat, speed);

}
...全文
301 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Go 旅城通票 2014-09-14
  • 打赏
  • 举报
回复
if (topTemp <= (distenceInit - distence)) { clearTimeout(t); //——这个怎么不起作用啊??? console.debug("end if,truely end......"); return } 结束执行,要不后面的 t = setTimeout(repeat, speed);又执行了
会飞的Pikachu 2014-09-14
  • 打赏
  • 举报
回复
用于清除定时的id
Go 旅城通票 2014-09-14
  • 打赏
  • 举报
回复
而且不用clearTimeout都行了。。主要是你最后那句导致计时器重新开始了 if (topTemp <= (distenceInit - distence)) { return }

87,904

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧