本文實(shí)例講述了JS實(shí)現(xiàn)HTML頁面中動(dòng)態(tài)顯示當(dāng)前時(shí)間。分享給大家供大家參考,具體如下:
<!doctype html><html><head><meta charset="utf-8"><title>www.survivalescaperooms.com JS動(dòng)態(tài)顯示時(shí)間</title> <script> var timer=null; function displayClock(num){//num是傳入的startClock中的動(dòng)態(tài)值 if(num<10){ return "0"+num; } else{ return num; } } //停止計(jì)時(shí) function stopClock(){ clearTimeout(timer); } //開始計(jì)時(shí) function startClock(){ var time =new Date(); var hours=displayClock(time.getHours())+":"; var minutes=displayClock(time.getMinutes())+":"; var seconds=displayClock(time.getSeconds()); //顯示時(shí)間 show.innerHTML=hours+minutes+seconds;//在id為show的塊區(qū)域顯示 timer=setTimeout("startClock()",1000);//延時(shí)器 } </script></head><style> #show{ font-size: 24px; color:#4213C9; text-align:center; } </style><body onload="startClock()" onunload="stopClock()"> <div id="show"></div></body></html>使用在線HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.VeVB.COm/code/HtmlJsRun,測(cè)試運(yùn)行效果如下:

PS:這里再為大家推薦幾款時(shí)間及日期相關(guān)工具供大家參考使用:
在線日期/天數(shù)計(jì)算器:
http://tools.VeVB.COm/jisuanqi/date_jisuanqi
在線日期計(jì)算器/相差天數(shù)計(jì)算器:
http://tools.VeVB.COm/jisuanqi/datecalc
在線日期天數(shù)差計(jì)算器:
http://tools.VeVB.COm/jisuanqi/onlinedatejsq
Unix時(shí)間戳(timestamp)轉(zhuǎn)換工具:
http://tools.VeVB.COm/code/unixtime
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript時(shí)間與日期操作技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注