JS實(shí)現(xiàn)一個(gè)秒表計(jì)時(shí)器
更新時(shí)間:2021年03月07日 15:03:06 作者:yiran0101001
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)一個(gè)秒表計(jì)時(shí)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了JS實(shí)現(xiàn)秒表計(jì)時(shí)器的具體代碼,供大家參考,具體內(nèi)容如下
秒表計(jì)時(shí)器的實(shí)現(xiàn):
效果圖如下:
附代碼,已調(diào)試運(yùn)行
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> #div1 { width: 300px; height: 400px; background: skyblue; margin: 100px auto; text-align: center; } #count { width: 200px; height: 150px; line-height: 150px; margin: auto; font-size: 40px; } #div1 input { width: 150px; height: 40px; background: orange; font-size: 25px; margin-top: 20px } </style> </head> <body> <div id="div1"> <div id="count"> <span id="id_H">00</span> <span id="id_M">00</span> <span id="id_S">00</span> </div> <input id="start" type="button" value="開始"> <input id="pause" type="button" value="暫停"> <input id="stop" type="button" value="停止"> </div> <script> //可以將查找標(biāo)簽節(jié)點(diǎn)的操作進(jìn)行簡化 var btn = getElementById('btn') function $(id) { return document.getElementById(id) } window.onload = function() { //點(diǎn)擊開始建 開始計(jì)數(shù) var count = 0 var timer = null //timer變量記錄定時(shí)器setInterval的返回值 $("start").onclick = function() { timer = setInterval(function() { count++; console.log(count) // 需要改變頁面上時(shí)分秒的值 console.log($("id_S")) $("id_S").innerHTML = showNum(count % 60) $("id_M").innerHTML = showNum(parseInt(count / 60) % 60) $("id_H").innerHTML = showNum(parseInt(count / 60 / 60)) }, 1000) } $("pause").onclick = function() { //取消定時(shí)器 clearInterval(timer) } //停止記數(shù) 數(shù)據(jù)清零 頁面展示數(shù)據(jù)清零 $("stop").onclick = function() { //取消定時(shí)器 $("pause").onclick() // clearInterval(timer) //數(shù)據(jù)清零 總秒數(shù)清零 count = 0 //頁面展示數(shù)據(jù)清零 $("id_S").innerHTML = "00" $("id_M").innerHTML = "00" $("id_H").innerHTML = "00" } //封裝一個(gè)處理單位數(shù)字的函數(shù) function showNum(num) { if (num < 10) { return '0' + num } return num } } </script> </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用mouse事件實(shí)現(xiàn)簡單的鼠標(biāo)經(jīng)過特效
這篇文章主要介紹了使用mouse事件實(shí)現(xiàn)簡單的鼠標(biāo)經(jīng)過特效的方法,需要的朋友可以參考下2015-01-01JS 中LocalStorage和SessionStorage的使用
最近因?yàn)轫?xiàng)目上需要使用到客戶端存儲,所以稍微研究了一下,以下說說自己的理解和使用經(jīng)驗(yàn),特此分享到腳本之家平臺,感興趣的朋友參考下吧2017-08-08js實(shí)現(xiàn)axios限制請求隊(duì)列
本文主要介紹了js實(shí)現(xiàn)axios限制請求隊(duì)列,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07