指定时间,倒计时效果
<div id="countdown"></div>
<script type="text/javascript">
function startCountdown(targetDate, display) {
var timer = setInterval(function () {
var currentDate = new Date();
var timeDiff = targetDate.getTime() - currentDate.getTime();
if (timeDiff <= 0) {
clearInterval(timer);
display.textContent = "已过期";
return;
}
var hours = Math.floor((timeDiff / (1000 * 60 * 60)) % 24);
var minutes = Math.floor((timeDiff / (1000 * 60)) % 60);
var seconds = Math.floor((timeDiff / 1000) % 60);
var countdownStr = hours + "小时 " + minutes + "分钟 " + seconds + "秒";
display.textContent = countdownStr;
}, 1000);
}
var countdown = document.getElementById("countdown");
var targetDate = new Date("2023-04-20T08:56:50+08:00");
startCountdown(targetDate, countdown);
</script>