setTimeout,延遲n秒后執行指定代碼
clearTimeout,清除計時器
<html> <head> <script type="text/javascript"> var c=0 var t function timedCount() { document.getElementById('txt').value=c c=c+1 t=setTimeout("timedCount()",1000) } function stopCount() { clearTimeout(t) } </script> </head> <body> <form> <input type="button" value="開始計時!" onClick="timedCount()"> <input type="text" id="txt"> <input type="button" value="停止計時!" onClick="stopCount()"> </form> <p> 請點擊上面的“開始計時”按鈕。輸入框會從 0 開始一直進行計時。點擊“停止計時”可停止計時。 </p> </body> </html>
參考:
https://blog.csdn.net/ainuser/article/details/78882473