为什么clearTimeout不能自动停止

yjxf8285 2011-11-01 11:41:41
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <head>
  <script type="text/javascript">
  var c=0;
  var t;
  function timedCount()
  {
if(c>=20){stopCount();};
  document.getElementById('txt').value=c;
  c++;
  t=setTimeout("timedCount()",100);
  }
  function stopCount()
  {
  clearTimeout(t);
  }
  </script>
  </head>
  <body>
  <form>
  <input type="button" value="Start count!" onClick="timedCount()"><input type="text" id="txt">
  <input type="button" value="Stop count!" onClick="stopCount()">
  </form>
  </body>
  </html>

点击按钮的时候就可以停止 但是用if来判断c>20的时候就不能停止,哪里错了?
...全文
142 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ybice 2011-11-01
  • 打赏
  • 举报
回复
stopCount();
后面加一个
return false;
yjxf8285 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zell419 的回复:]

HTML code

function timedCount()
  {
if(c>=20){
stopCount();
}
else{
c++;
t=setTimeout("timedCount()",100);
}
   document.getEl……
[/Quote]

明白了,我没有终止timedCount()的执行。再追问一句,有没有什么方法可以终止指定的函数执行,比如stopfn(timedCount) ---我自己瞎编的,你懂的!
zell419 2011-11-01
  • 打赏
  • 举报
回复

function timedCount()
  {
if(c>=20){
stopCount();
}
else{
c++;
t=setTimeout("timedCount()",100);
}
   document.getElementById('txt').value=c;
  }

这样就可以了 。

87,990

社区成员

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

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