<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>计时器</title>
<style>
html,body{height:100%;width:100%;background-color:rgb(48, 65, 86);}
.btn{background-color:#409EFF;color:#ddd;padding:3px 7px;border:0;border-radius:5px;letter-spacing:2px;cursor:pointer;}
#box{height:40px;padding:10px;width:20px;color:#fff;font-size:20px;line-height:40px;}
</style>
</head>
<body>
<div id="box">00:00:00:000</div>
<div class="control">
<span class="btn" onclick="setTimer('begin')">开始</span>
<span class="btn" onclick="setTimer('suspend')">暂停</span>
<span class="btn" onclick="setTimer('reset')">重置</span>
</div>
<script>
let hour=0,minute=0,second=0,millisecond=0;
let box = document.getElementById('box');
let Interval;
function setTimer(e){
if(e=='begin'){
Interval=setInterval(timer,50);
}else if(e=='suspend'){
window.clearInterval(Interval);
}else{
hour=00;minute=00;second=00;millisecond=00;
window.clearInterval(Interval);
box.innerHTML = '00:00:00:000';
}
}
function timer(){
millisecond+=50;
if(millisecond>=1000)
{
millisecond=0;
second++;
}
if(second>=60)
{
second=0;
minute++;
}
if(minute>=60)
{
minute=0;
hour++;
}
if(hour<10){
hour=PrefixInteger(hour,2);
}
if(minute<10){
minute=PrefixInteger(minute,2);
}
if(second<10){
second=PrefixInteger(second,2);
}
box.innerHTML=hour+':'+minute+':'+second+':'+millisecond;
}
function PrefixInteger(num, length) {
return (Array(length).join('0') + num).slice(-length);
}
</script>
</body>
</html>
答案满意记得采纳,谢谢
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>计时器</title>
<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=hour+'时'+minute+'分'+second+'秒'+millisecond+'毫秒';
}
function stop()//暂停
{
window.clearInterval(int);
}
</script>
</head>
<body>
<div style="text-align: center">
<input type="text" id="timetext" value="00时00分00秒" readonly><br>
<button type="button" onclick="start()">开始</button> <button type="button" onclick="stop()">暂停</button> <button type="button" onclick="Reset()">重置</button>
</div>
</body>
</html>