无限循环中的计时事件 - 带有停止按钮
- <!DOCTYPE html>
- <html>
- <body>
- <button onClick="myTimer = setInterval(myCounter, 1000)">启动计数器</button>
- <p id="demo">点击上面的按钮,我会永远计数。</p>
- <button onClick="clearInterval(myTimer)">停止计数器</button>
- <script>
- var c = 0;
- function myCounter() {
- document.getElementById("demo").innerHTML = ++c;
- }
- </script>
- </body>
- </html>