以下是一个使用 JavaScript 编写的简单倒计时程序:
// 设置倒计时的结束时间
let endTime = new Date().getTime() + 60000; // 假设倒计时为 60 秒
function updateCountdown() {
let now = new Date().getTime();
let distance = endTime - now;
if (distance < 0) {
clearInterval(interval);
console.log("倒计时结束");
return;
}
let seconds = Math.floor((distance % (1000 * 60)) / 1000);
console.log(seconds + " 秒");
}
let interval = setInterval(updateCountdown, 1000);
基础概念:
Date
对象用于处理日期和时间。setInterval
函数用于定期执行某个函数。优势:
类型:
应用场景:
如果遇到问题:
localStorage
中。领取专属 10元无门槛券
手把手带您无忧上云