无限循环中的计时事件

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <button onClick="setInterval(myCounter, 1000)">启动计数器</button>
  6.  
  7. <p id="demo">点击上面的按钮,我会永远计数。</p>
  8.  
  9. <script>
  10. var c = 0;
  11. function myCounter() {
  12. document.getElementById("demo").innerHTML = ++c;
  13. }
  14. </script>
  15.  
  16. </body>
  17. </html>