JavaScript實(shí)現(xiàn)倒計(jì)時(shí)功能,附實(shí)現(xiàn)源碼
在 JavaScript 中實(shí)現(xiàn)倒計(jì)時(shí)可以通過(guò) setInterval 或 setTimeout 來(lái)實(shí)現(xiàn)。以下是一個(gè)簡(jiǎn)單的倒計(jì)時(shí)示例,支持天、小時(shí)、分鐘和秒的顯示。
 
在 JavaScript 中實(shí)現(xiàn)倒計(jì)時(shí)可以通過(guò) setInterval 或 setTimeout 來(lái)實(shí)現(xiàn)。以下是一個(gè)簡(jiǎn)單的倒計(jì)時(shí)示例,支持天、小時(shí)、分鐘和秒的顯示。
代碼
function countdown(targetDate, callback) {
    // 目標(biāo)時(shí)間
    const targetTime = newDate(targetDate).getTime();
    // 每秒更新一次倒計(jì)時(shí)
    const timer = setInterval(() => {
        // 當(dāng)前時(shí)間
        const now = newDate().getTime();
        // 剩余時(shí)間(毫秒)
        const timeRemaining = targetTime - now;
        // 如果倒計(jì)時(shí)結(jié)束
        if (timeRemaining <= 0) {
            clearInterval(timer); // 停止計(jì)時(shí)器
            callback("倒計(jì)時(shí)結(jié)束!"); // 執(zhí)行回調(diào)
            return;
        }
        // 計(jì)算天、小時(shí)、分鐘、秒
        const days = Math.floor(timeRemaining / (1000 * 60 * 60 * 24));
        const hours = Math.floor((timeRemaining % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        const minutes = Math.floor((timeRemaining % (1000 * 60 * 60)) / (1000 * 60));
        const seconds = Math.floor((timeRemaining % (1000 * 60)) / 1000;
        // 調(diào)用回調(diào)函數(shù),返回剩余時(shí)間
        callback({ days, hours, minutes, seconds });
    }, 1000); // 每秒更新一次
}
// 示例用法
const targetDate = "2023-12-31T23:59:59"; // 目標(biāo)時(shí)間
countdown(targetDate, (time) => {
    if (typeof time === "string") {
        console.log(time); // 倒計(jì)時(shí)結(jié)束
    } else {
        console.log(
            `剩余時(shí)間:${time.days}天 ${time.hours}小時(shí) ${time.minutes}分鐘 ${time.seconds}秒`
        );
    }
});代碼說(shuō)明
- targetDate:目標(biāo)時(shí)間,可以是字符串(如 "2023-12-31T23:59:59")或 Date 對(duì)象。
 - setInterval:每秒執(zhí)行一次,更新倒計(jì)時(shí)。
 - timeRemaining:計(jì)算剩余時(shí)間(毫秒)。
 - days, hours, minutes, seconds:將剩余時(shí)間轉(zhuǎn)換為天、小時(shí)、分鐘和秒。
 - callback:回調(diào)函數(shù),用于返回倒計(jì)時(shí)結(jié)果或結(jié)束提示。
 
示例輸出
剩余時(shí)間:30天 5小時(shí) 10分鐘 45秒
剩余時(shí)間:30天 5小時(shí) 10分鐘 44秒
剩余時(shí)間:30天 5小時(shí) 10分鐘 43秒
...
倒計(jì)時(shí)結(jié)束!優(yōu)化:格式化時(shí)間
如果希望時(shí)間顯示為兩位數(shù)(如 01 而不是 1),可以添加一個(gè)格式化函數(shù):
function formatTime(time) {
    return time < 10 ? `0${time}` : time;
}
// 在回調(diào)中使用
countdown(targetDate, (time) => {
    if (typeof time === "string") {
        console.log(time);
    } else {
        console.log(
            `剩余時(shí)間:${formatTime(time.days)}天 ${formatTime(time.hours)}小時(shí) ${formatTime(time.minutes)}分鐘 ${formatTime(time.seconds)}秒`
        );
    }
});停止倒計(jì)時(shí)
如果需要手動(dòng)停止倒計(jì)時(shí),可以返回 clearInterval 的函數(shù):
function countdown(targetDate, callback) {
    const targetTime = newDate(targetDate).getTime();
    const timer = setInterval(() => {
        const now = newDate().getTime();
        const timeRemaining = targetTime - now;
        if (timeRemaining <= 0) {
            clearInterval(timer);
            callback("倒計(jì)時(shí)結(jié)束!");
            return;
        }
        const days = Math.floor(timeRemaining / (1000 * 60 * 60 * 24));
        const hours = Math.floor((timeRemaining % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        const minutes = Math.floor((timeRemaining % (1000 * 60 * 60)) / (1000 * 60));
        const seconds = Math.floor((timeRemaining % (1000 * 60)) / 1000);
        callback({ days, hours, minutes, seconds });
    }, 1000);
    // 返回停止函數(shù)
    return() =>clearInterval(timer);
}
// 示例用法
const stop = countdown(targetDate, (time) => {
    if (typeof time === "string") {
        console.log(time);
    } else {
        console.log(
            `剩余時(shí)間:${time.days}天 ${time.hours}小時(shí) ${time.minutes}分鐘 ${time.seconds}秒`
        );
    }
});
// 手動(dòng)停止倒計(jì)時(shí)
setTimeout(() => {
    stop();
    console.log("倒計(jì)時(shí)已停止!");
}, 5000); // 5秒后停止總結(jié)
- 使用 setInterval 實(shí)現(xiàn)倒計(jì)時(shí)。
 - 支持天、小時(shí)、分鐘、秒的顯示。
 - 可以通過(guò)回調(diào)函數(shù)返回倒計(jì)時(shí)結(jié)果或結(jié)束提示。
 - 提供格式化時(shí)間和手動(dòng)停止的功能。
 
責(zé)任編輯:龐桂玉 
                    來(lái)源:
                    web前端開(kāi)發(fā)
 














 
 
 








 
 
 
 