JS实现挑战10秒 主要用到setInterval计时器
效果图
## 完整代码
<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);
}
function oReset() {
txt.innerText="00:00";
n=0;
}
function toDub(n){
return n<10?"0"+n:n;
}
</script>
</body>
</html>