自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

JavaScript實(shí)現(xiàn)倒計(jì)時(shí)功能,附實(shí)現(xiàn)源碼

開發(fā) 前端
在 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ō)明

  1. targetDate:目標(biāo)時(shí)間,可以是字符串(如 "2023-12-31T23:59:59")或 Date 對(duì)象。
  2. setInterval:每秒執(zhí)行一次,更新倒計(jì)時(shí)。
  3. timeRemaining:計(jì)算剩余時(shí)間(毫秒)。
  4. days, hours, minutes, seconds:將剩余時(shí)間轉(zhuǎn)換為天、小時(shí)、分鐘和秒。
  5. 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前端開發(fā)
相關(guān)推薦

2014-08-18 14:30:27

Android倒計(jì)時(shí)

2022-10-21 15:42:21

倒計(jì)時(shí)鴻蒙

2015-01-21 16:07:57

Android源碼驗(yàn)證碼倒計(jì)時(shí)

2011-04-11 09:17:28

Ubuntu倒計(jì)時(shí)

2017-07-20 16:21:52

UICountDownTidelay

2015-03-23 17:58:04

驗(yàn)證碼倒計(jì)時(shí)并行

2014-03-21 13:46:45

2022-06-29 21:22:49

CSS動(dòng)感倒計(jì)時(shí)

2011-04-11 09:50:56

Ubuntu 11.0

2014-02-18 10:36:33

2015-09-16 13:19:46

javascript服務(wù)器

2013-04-09 10:01:18

微軟Windows XP

2013-10-10 09:23:15

Android 4.4Kitkat

2019-12-13 19:37:00

BashLinux命令

2020-10-28 17:54:49

成都信息安全

2013-10-08 09:24:39

Windows 8.1Windows 8

2022-06-14 08:45:27

瀏覽器IEWindows

2021-11-26 00:00:13

AndroidCountDownTi控件

2013-06-06 11:27:52

iRadioWWDC2013
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)