DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Timertitle>
<style>
body{
height: 400px;
width: 305px;
margin: 0 auto;
margin-top: 200px;
}
.header{
width: 304px;
height: 20px;
}
.box{
margin-top: 20px;
width: 304px;
height: 104px;
border: 2px solid #000000;
box-sizing: border-box;
}
.hour,
.minute,
.second,
.point{
float: left;
display: flex;
align-items: center;
justify-content: center;
}
.hour,
.minute,
.second{
width: 80px;
height: 100px;
font-size: 30px;
}
input{
height: 20px;
width: 74px;
}
.point{
font-size: 30px;
float: left;
width: 30px;
height: 100px;
}
style>
head>
<body>
<div class="header">
<input type="number" min ="0" max="23" placeholder="小时" value="0" id="hour">
<input type="number" min ="0" max="59" placeholder="分钟" value="0" id="minute">
<input type="number" min ="0" max="59" placeholder="秒" value="0" id="second">
<button disabled>开始button>
div>
<div class="box">
<div class="hour">0div>
<div class="point">:div>
<div class="minute">0div>
<div class="point">:div>
<div class="second">0div>
div>
<script>
let btn=document.querySelector("button");
let hour=document.querySelector(".hour");
let minute=document.querySelector(".minute");
let second=document.querySelector(".second");
btn.disabled=false;
btn.addEventListener("click",function(){
let inputHour=parseInt(document.querySelector("#hour").value);
let inputMinute=parseInt(document.querySelector("#minute").value);
let inputSecond=parseInt(document.querySelector("#second").value);
if(inputHour<24&&inputMinute<60&&inputSecond<60){
btn.disabled=true;
let timer=setInterval(function(){
hour.innerHTML=`${inputHour}`;
minute.innerHTML=`${inputMinute}`;
second.innerHTML=`${inputSecond}`;
if(inputMinute===0&&inputHour===0&&inputSecond===0){
clearInterval(timer);
btn.disabled=false;
}
else if(inputSecond===0&&inputMinute>0){
inputMinute--;
inputSecond=59;
}
else if(inputMinute===0&&inputHour>0&&inputSecond===0){
inputHour--;
inputMinute=59;
inputSecond=59;
}
inputSecond--;
},1000);
}else{
alert("输入的数字不符合时间设定,请重新输入");
}
});
script>
body>
html>