js编写简单的计时器功能

小白一枚,如有代码不规范或者写错的地方,希望得大神指导

js编写简单的计时器功能

代码:

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /> <title></title> </head> <body> <div></div> <script src="https://www.jb51.net/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>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wyjxxz.html