JavaScript实现倒计时功能,附实现源码

开发 前端
在 JavaScript 中实现倒计时可以通过 setInterval 或 setTimeout 来实现。以下是一个简单的倒计时示例,支持天、小时、分钟和秒的显示。

在 JavaScript 中实现倒计时可以通过 setInterval 或 setTimeout 来实现。以下是一个简单的倒计时示例,支持天、小时、分钟和秒的显示。

代码

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("倒计时结束!"); // 执行回调
            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); // 每秒更新一次
}

// 示例用法
const targetDate = "2023-12-31T23:59:59"; // 目标时间
countdown(targetDate, (time) => {
    if (typeof time === "string") {
        console.log(time); // 倒计时结束
    } else {
        console.log(
            `剩余时间:${time.days}天 ${time.hours}小时 ${time.minutes}分钟 ${time.seconds}秒`
        );
    }
});
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.

代码说明

  1. targetDate:目标时间,可以是字符串(如 "2023-12-31T23:59:59")或 Date 对象。
  2. setInterval:每秒执行一次,更新倒计时。
  3. timeRemaining:计算剩余时间(毫秒)。
  4. days, hours, minutes, seconds:将剩余时间转换为天、小时、分钟和秒。
  5. callback:回调函数,用于返回倒计时结果或结束提示。

示例输出

剩余时间:30天 5小时 10分钟 45秒
剩余时间:30天 5小时 10分钟 44秒
剩余时间:30天 5小时 10分钟 43秒
...
倒计时结束!
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

优化:格式化时间

如果希望时间显示为两位数(如 01 而不是 1),可以添加一个格式化函数:

function formatTime(time) {
    return time < 10 ? `0${time}` : time;
}

// 在回调中使用
countdown(targetDate, (time) => {
    if (typeof time === "string") {
        console.log(time);
    } else {
        console.log(
            `剩余时间:${formatTime(time.days)}天 ${formatTime(time.hours)}小时 ${formatTime(time.minutes)}分钟 ${formatTime(time.seconds)}秒`
        );
    }
});
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

停止倒计时

如果需要手动停止倒计时,可以返回 clearInterval 的函数:

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("倒计时结束!");
            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);

    // 返回停止函数
    return() =>clearInterval(timer);
}

// 示例用法
const stop = countdown(targetDate, (time) => {
    if (typeof time === "string") {
        console.log(time);
    } else {
        console.log(
            `剩余时间:${time.days}天 ${time.hours}小时 ${time.minutes}分钟 ${time.seconds}秒`
        );
    }
});

// 手动停止倒计时
setTimeout(() => {
    stop();
    console.log("倒计时已停止!");
}, 5000); // 5秒后停止
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.

总结

  • 使用 setInterval 实现倒计时。
  • 支持天、小时、分钟、秒的显示。
  • 可以通过回调函数返回倒计时结果或结束提示。
  • 提供格式化时间和手动停止的功能。
责任编辑:庞桂玉 来源: web前端开发
相关推荐

2014-08-18 14:30:27

Android倒计时

2022-10-21 15:42:21

倒计时鸿蒙

2015-01-21 16:07:57

Android源码验证码倒计时

2011-04-11 09:17:28

Ubuntu倒计时

2015-03-23 17:58:04

验证码倒计时并行

2017-07-20 16:21:52

UICountDownTidelay

2014-03-21 13:46:45

2022-06-29 21:22:49

CSS动感倒计时

2011-04-11 09:50:56

Ubuntu 11.0

2014-02-18 10:36:33

2015-09-16 13:19:46

javascript服务器

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

2017-02-09 16:35:17

戴尔

2019-11-22 11:54:04

数字时代数字中国数字产业
点赞
收藏

51CTO技术栈公众号