HTML DOM clearTimeout() 方法

定义和用法

clearTimeout() 方法可取消由 setTimeout() 方法设置的 timeout。

语法

  1. clearTimeout(id_of_settimeout)
参数 描述
id_of_settimeout 由 setTimeout() 返回的 ID 值。该值标识要取消的延迟执行代码块。

实例

下面的例子每秒调用一次 timedCount() 函数。您也可以使用一个按钮来终止这个定时消息:

  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. var c=0
  5. var t
  6. function timedCount()
  7. {
  8. document.getElementById('txt').value=c
  9. c=c+1
  10. t=setTimeout("timedCount()",1000)
  11. }
  12. function stopCount()
  13. {
  14. clearTimeout(t)
  15. }
  16. </script>
  17. </head>
  18. <body>
  19.  
  20. <form>
  21. <input type="button" value="Start count!" onClick="timedCount()">
  22. <input type="text" id="txt">
  23. <input type="button" value="Stop count!" onClick="stopCount()">
  24. </form>
  25.  
  26. </body>
  27. </html>