<div>
<span class="hour">1</span>
<span class="minute">2</span>
<span class="second">3</span>
</div>
<script>
let hour = document.querySelector('.hour')
let minute = document.querySelector('.minute')
let second = document.querySelector('.second')
let inputTime = +new Date('2022-1-31 20:00:00')//返回用户输入时间总的毫秒数
countDown()//先调用一次函数 防止第一次刷新页面有空白
// 开启定时器
setInterval(countDown, 1000)
function countDown() {
let nowTime = +new Date()//返回的是当前时间总的毫秒数
let times = (inputTime - nowTime) / 1000 //times是剩余时间总的秒数
let h = parseInt(times / 60 / 60 % 24)
h = h < 10 ? '0' + h : h
hour.innerHTML = h
let m = parseInt(times / 60 % 60)
m = m < 10 ? '0' + m : m
minute.innerHTML = m
let s = parseInt(times % 60)
s = s < 10 ? '0' + s : s
second.innerHTML = s
}
</script>