JS使用setInterval計時器實現挑戰10秒
JS實現挑戰10秒,主要用到setInterval計時器,供大家參考,具體內容如下
效果圖
## 完整代碼
<html lang='en'><head><meta charset='UTF-8'><title>js計時器</title></head><body><p style='font-size: 2em;color: blue;font-style: italic;'>挑戰10.00秒</p><p style='font-size: 2em;color: red;'>00:00</p><input type='button' value='開始' onclick='oStart()'><input type='button' value='結束' onclick='oStop()'><input type='button' value='重置' onclick='oReset()'><script> var n= 0, timer=null; var txt=document.getElementById('time'); //開始計時 function oStart() { clearInterval(timer); timer=setInterval(function () { n++; var m=parseInt(n/60); var s=parseInt(n%60); txt.innerText=toDub(m)+':'+toDub(s); },1000/60); }; //暫停并且清空計時器 function oStop() { clearInterval(timer);// txt.innerText='我愛你'; } //重置 function oReset() { txt.innerText='00:00'; n=0; } //補零 function toDub(n){ return n<10?'0'+n:n; }</script></body></html>
更多關于倒計時的文章請查看專題: 《倒計時功能》
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章: