<body>
<div id="box"></div>
<script>
setInterval(function () {
var now = new Date(); //当前时间
var targetDate = new Date('2023-08-24 22:00:00'); //目标时间
var res = targetDate - now;
var secsCount = parseInt(res / 1000); //相差的总的秒数
var secs = secsCount % 60;
var miusCount = parseInt(secsCount / 60); //相差的总的分钟数
var mius = miusCount % 60;
var hoursCount = parseInt(miusCount / 60);
var result =
addZero(hoursCount) +
'时' +
addZero(mius) +
'分' +
addZero(secs) +
'秒';
box.innerHTML = result;//将字符串显示到div中
}, 1000);
function addZero(num) {
return (num = num > 10 ? num : '0' + num);
}
</script>
</body>
使用js设置倒计时
于 2023-08-24 17:31:13 首次发布