日落沧海看夕阳,万丈霞光谱辉煌。轻叹美景难留住,只因月色要锋芒!你在桥上看风景,看风景的人在桥下看你。你只知道自己吃了韭菜盒子,却不知道别人在盒子里还加了鸡蛋。
本文实例讲述了JS实现HTML页面中动态显示当前时间。分享给大家供大家参考,具体如下:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>www.haodaima.com JS动态显示时间</title> <script> var timer=null; function displayClock(num){//num是传入的startClock中的动态值 if(num<10){ return "0"+num; } else{ return num; } } //停止计时 function stopClock(){ clearTimeout(timer); } //开始计时 function startClock(){ var time =new Date(); var hours=displayClock(time.getHours())+":"; var minutes=displayClock(time.getMinutes())+":"; var seconds=displayClock(time.getSeconds()); //显示时间 show.innerHTML=hours+minutes+seconds;//在id为show的块区域显示 timer=setTimeout("startClock()",1000);//延时器 } </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代码运行工具:http://tools.haodaima.com/code/HtmlJsRun,测试运行效果如下:
PS:这里再为大家推荐几款时间及日期相关工具供大家参考使用:
在线日期/天数计算器:
http://tools.haodaima.com/jisuanqi/date_jisuanqi
在线日期计算器/相差天数计算器:
http://tools.haodaima.com/jisuanqi/datecalc
在线日期天数差计算器:
http://tools.haodaima.com/jisuanqi/onlinedatejsq
Unix时间戳(timestamp)转换工具:
http://tools.haodaima.com/code/unixtime
希望本文所述对大家JavaScript程序设计有所帮助。
以上就是JS实现HTML页面中动态显示当前时间完整示例。小时候,幸福是一件实物;长大后,幸福是一种状态;然后,突然有一天,我发现,幸福既不是实物,也不是状态,是一种领悟。更多关于JS实现HTML页面中动态显示当前时间完整示例请关注haodaima.com其它相关文章!