[JavaScript] JavaScript通过setTimeout和clearTimeout控制定时器的开关 →→→→→进入此内容的聊天室

来自 , 2019-11-26, 写在 JavaScript, 查看 159 次.
URL http://www.code666.cn/view/d6dabcc4
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script>
  5. var c=0;
  6. var t;
  7. var timer_is_on=0;
  8.  
  9. function timedCount()
  10. {
  11. document.getElementById('txt').value=c;
  12. c=c+1;
  13. t=setTimeout(function(){timedCount()},1000);
  14. }
  15.  
  16. function doTimer()
  17. {
  18. if (!timer_is_on)
  19.   {
  20.   timer_is_on=1;
  21.   timedCount();
  22.   }
  23. }
  24.  
  25. function stopCount()
  26. {
  27. clearTimeout(t);
  28. timer_is_on=0;
  29. }
  30. </script>
  31. </head>
  32.  
  33. <body>
  34. <form>
  35. <input type="button" value="Start count!" onclick="doTimer()" />
  36. <input type="text" id="txt" />
  37. <input type="button" value="Stop count!" onclick="stopCount()" />
  38. </form>
  39. <p>
  40. Click on the "Start count!" button above to start the timer. The input field will count forever, starting at 0. Click on the "Stop count!" button to stop the counting. Click on the "Start count!" button to start the timer again.
  41. </p>
  42. </body>
  43. </html>
  44.  
  45. //javascript/7889

回复 "JavaScript通过setTimeout和clearTimeout控制定时器的开关"

这儿你可以回复上面这条便签

captcha