JS实现挑战10秒,主要用到setInterval计时器,供大家参考,具体内容如下
效果图
## 完整代码
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
|
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< title >js计时器</ title >
</ head >
< body >
< p style = "font-size: 2em;color: blue;font-style: italic;" >挑战10.00秒</ p >
< p id = "time" style = "font-size: 2em;color: red;" >00:00</ p >
< input type = "button" value = "开始" onclick = "oStart()" >
< input type = "button" value = "结束" onclick = "oStop()" >
< input type = "button" value = "重置" onclick = "oReset()" >
< script >
var n= 0, timer=null;
var txt=document.getElementById("time");
//开始计时
function oStart() {
clearInterval(timer);
timer=setInterval(function () {
n++;
var m=parseInt(n/60);
var s=parseInt(n%60);
txt.innerText=toDub(m)+":"+toDub(s);
},1000/60);
};
//暂停并且清空计时器
function oStop() {
clearInterval(timer);
// txt.innerText="我爱你";
}
//重置
function oReset() {
txt.innerText="00:00";
n=0;
}
//补零
function toDub(n){
return n< 10 ?"0"+n:n;
}
</script>
</ body >
</ html >
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_41605893/article/details/109540131