JavaScript實現(xiàn)簡單計時器
更新時間:2021年06月22日 09:13:44 作者:妄癡夢中
這篇文章主要為大家詳細介紹了JavaScript實現(xiàn)簡單計時器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了JavaScript實現(xiàn)簡單計時器的具體代碼,供大家參考,具體內容如下
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>計時器</title> <style> .bigDiv { margin: 50px auto; width: 200px; height: 200px; background-color: lightskyblue; border-radius: 10px; } #showNum { width: 200px; height: 20px; background-color: orange; text-align-all: center; border-radius: 5px; } </style> </head> <body> <div class="bigDiv"> <h2 align="center">計時器</h2> <div id="showNum" align="center"> 0 </div> <br> <br> <div class="butDiv">     <button type="button" id="start">開始</button>   <button type="button" id="stop">停止</button>   <button type="button" id="reset">復位</button>   </div> </div> <script> //定義顯示的時間 let int = null; /** * 開始 單擊事件 */ document.getElementById("start").addEventListener('click', function () { if (int == null) { //設置定時器 //每隔參數(shù)二毫秒執(zhí)行一次參數(shù)一 int = setInterval(startNum, 1000); } }); /** * 停止 單擊事件 */ document.getElementById("stop").addEventListener('click', function () { //清除定時器, clearInterval(int); int = null; }); /** * 復位 單擊事件 */ let num = 0; document.getElementById("reset").addEventListener('click', function () { if (int == null) { num = 0; //將時間變成0 document.getElementById("showNum").innerHTML = num; } }); function startNum() { num++; //持續(xù)改變時間 document.getElementById("showNum").innerHTML = num; } </script> </body> </html>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
詳談for循環(huán)里面的break和continue語句
下面小編就為大家?guī)硪黄斦刦or循環(huán)里面的break和continue語句。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07JavaScript實現(xiàn)基于十進制的四舍五入實例
這篇文章主要介紹了JavaScript實現(xiàn)基于十進制的四舍五入的方法,實例分析了javascript針對數(shù)值判斷與相關運算技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07Bootstrap DateTime Picker日歷控件簡單應用
這篇文章主要介紹了Bootstrap DateTime Picker日歷控件的簡單應用,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03JavaScript獲取URL中參數(shù)querystring的方法詳解
這篇文章先給大家介紹了JavaScript獲取URL中參數(shù)querystring的方法,而后有詳解介紹了Location對象的屬性和,Location對象的方法,對大家的理解很有幫助,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-10-10