宝哥软件园

JS使用setInterval实现简单定时器功能的例子

编辑:宝哥软件园 来源:互联网 时间:2021-08-31

本文举例说明了JS使用setInterval实现的简单定时器功能。分享给大家参考,如下:

使用setInterval实现计时,60秒后提前一到分钟,60分钟后提前一到小时。

!doctype HTML HTML Head metachartset=' UTF-8 ' titlewww.jb51.net JS定时器/Title脚本window . onload=function(){ var mm=0;var ss=0;var str=var timer=setInterval(function(){ str=' ';if(ss==60){ if(mm==60){ mm=0;} ss=0;} str=mm10?0 ' mm:mmstr=' :str=ss10?0 ' ss:ssdocument.getElementById('d ')。innerHTML=str},1000);};/script/head body div id=' d '/div/body/html run结果:

PS:这里我们推荐几个时间和日期相关的工具,供大家参考:

在线秒表工具:http://tools.jb51.net/bianmin/miaobiao

在线日期/天数计算器:http://tools.jb51.net/jisuanqi/date_jisuanqi

在线日期和天数差异计算器:http://tools.jb51.net/jisuanqi/onlinedatejsq

Unix时间戳转换工具:http://tools.jb51.net/code/unixtime

更多对JavaScript相关内容感兴趣的读者可以查看本网站专题:《JavaScript时间与日期操作技巧总结》、《JavaScript查找算法技巧总结》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》、《JavaScript数学运算用法总结》、0103010

希望本文对JavaScript编程有所帮助。

更多资讯
游戏推荐
更多+