js中setTimeout和clearTimeout的使用


 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM