HTML:
<div>
<span></span>
<span></span>
<span></span>
<span></span>
</div>
JS:
<script>
var spans = document.querySelectorAll('span');
timeLive = +new Date('2020-3-23 00:00:00')
//倒计时函数
function countdown() {
noWTime = +new Date;
times = (timeLive - noWTime) / 1000;
day = parseInt(times / 60 / 60 / 24);
spans[0].innerText = '还剩下' + day + '天'
h = parseInt(times / 60 / 60 % 24);
h = h < 10 ? '0' + h : h;
spans[1].innerText = h + '时'
m = parseInt(times / 60 % 60);
m = m < 10 ? '0' + m : m;
spans[2].innerText = m + '分'
s = parseInt(times % 60);
s = s < 10 ? '0' + s : s;
spans[3].innerText = s + '秒'
}
//需要先调用一下函数,这样页面一打开就把时间显示出来,不然需要等1秒再执行
countdown()
setInterval(countdown, 1000)