本文实例为大家分享了js实现计时器秒表功能的具体代码,供大家参考,具体内容如下
HTML
1 2 3 4 | < input type = "text" id = "timetext" value = "00:00:00" readonly> < button type = "button" onclick = "start()" >开始</ button > < button type = "button" onclick = "stop()" >暂停</ button > < button type = "button" onclick = "Reset()" >重置</ button > |
Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <script> var hour,minute,second; //时 分 秒 hour=minute=second=0; //初始化 var millisecond=0; //毫秒 var int; function Reset() //重置 { window.clearInterval(int); millisecond=hour=minute=second=0; document.getElementById( 'timetext' ).value= '00:00:00:000' ; } function start() //开始 { int=setInterval(timer,50); } function timer() //计时 { millisecond=millisecond+50; if (millisecond>=1000) { millisecond=0; second=second+1; } if (second>=60) { second=0; minute=minute+1; } if (minute>=60) { minute=0; hour=hour+1; } document.getElementById( 'timetext' ).value=toDub(hour)+ ':' +toDub(minute)+ ':' +toDub(second); } function stop() //暂停 { window.clearInterval(int); } //补零 function toDub(n){ return n<10? "0" +n: "" +n; } </script> |
如图:
![](https://i-blog.csdnimg.cn/blog_migrate/de7a41d8e266cbe788e68b90dc9061ab.jpeg)