js编写简单的计时器功能
ouqi_qiou 人气:0本文实例为大家分享了js计时器功能的编写代码,供大家参考,具体内容如下
小白一枚,如有代码不规范或者写错的地方,希望得大神指导
代码:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /> <title></title> </head> <body> <div id="waitTime"></div> <script src="js/jquery-git.js"></script> <script type="text/javascript"> //计时 var time = 0; var a = setInterval(jishi, 1000); //1000毫秒 var isCanCancerOrder = false; function jishi() { time++; $('#waitTime').html(calTime(time)); //倒计时 if(time == 60) { //1分钟之后 alert("一分钟到啦!"); } } function calTime(time) { var spit = ":"; var hour = "00"; var second = "00"; var min = "00"; var result = ""; if(time % 60 != 0) { //秒 if(time % 60 > 10) { second = time % 60; } else { second = "0" + time % 60; } } if(parseInt(time / 60) != 0) { //分 if(parseInt(time / 60) > 10) { min = parseInt(time / 60); } else { min = "0" + parseInt(time / 60); } } if(parseInt(time / 3600) != 0) { //时 if(parseInt(time / 3600) > 10) { hour = parseInt(time / 3600); } else { hour = "0" + parseInt(time / 3600); } } result = hour + spit + min + spit + second; return result; } </script> </body> </html>
加载全部内容