npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

countdown-pro

v2.2.1

Published

A simple countdown.

Downloads

308

Readme

CountDown

npm npm GitHub

一个简单的倒计时。

查看示例

使用

安装

npm install countdown-pro
yarn add countdown-pro
pnpm add countdown-pro

浏览器引入

在浏览器中使用 script 标签直接引入文件,并使用全局变量 CountDown

  • 该仓库的 dist 目录下提供了 countdown.umd.js 以及 countdown.umd.min.js
  • npm 包的 countdown-pro/dist 目录下也提供了 countdown.umd.js 以及 countdown.umd.min.js
  • 你也可以通过 UNPKG 进行下载。

示例

import CountDown from 'countdown-pro';

const countdown = new CountDown({
  time: 60 * 1000,
  interval: 1000,
  onChange(time) {
    console.log(time);
  },
  onEnd() {
    console.log('结束');
  }
});

countdown.start();

配置项

| 参数 | 说明 | 类型 | 必填 | 默认值 | | --- | --- | --- | --- | --- | | time | 倒计时,单位毫秒 | number | Y | 0 | | interval | 时间间隔,单位毫秒 | number | N | 1000 | | onChange | 倒计时时间变动时触发 | (currentTime: number) => void | N | - | | onEnd | 倒计时结束时触发 | () => void | N | - | | adjustInterval | 自动校准倒计时时间间隔,单位毫秒。如果有值且大于 0,再开始倒计时时会执行自动校准定时任务。暂定或结束倒计时会停止定时任务。 | number | N | - |

实例方法

| 方法名 | 说明 | | ------------- | ----------------------------------------------------------------------------- | | start | 开始倒计时 | | pause | 暂停倒计时 | | reset | 重置倒计时。先暂停再将倒计时时间重置。 | | restart | 重置再开始倒计时 | | updateOptions | 更新配置。如果更新 time 需要手动调用 resetrestart 方法才生效。 | | adjustTime | 校准倒计时。仅在倒计时运行时才生效,如果需要校准会先暂停再开始。 |

静态方法

内置一些简单日期格式方法,通过 CountDown.方法名 直接调用。

CountDown.format(timestamp, pattern='HH:mm:ss')

格式化时间,返回格式化后的时间字符串

CountDown.format(2 * 60 * 60 * 1000); // "02:00:00"
CountDown.format(2 * 60 * 60 * 1000, 'mm:ss'); // "120:00"

| 参数 | 说明 | 类型 | 必填 | 默认值 | | --------- | ---------------------------------------------- | -------- | ---- | ---------- | | timestamp | 时间戳,单位毫秒 | number | Y | - | | pattern | 时间格式,DD-日,HH-时,mm-分,ss-秒,SSS-毫秒 | string | - | HH:mm:ss |

CountDown.padZero(num, targetLength=2)

前置补零,返回补零后的值

CountDown.padZero(2); // "02"

CountDown.parseTimeData(timestamp)

解析时间戳,返回的时间对象格式 timeData

type TimeData = {
  days: number; // 天数
  hours: number; // 小时
  minutes: number; // 分钟
  seconds: number; // 秒数
  milliseconds: number; // 毫秒
};
CountDown.parseTimeData(2 * 60 * 60 * 1000); // {days: 0, hours: 2, minutes: 0, seconds: 0, milliseconds: 0}

常见问题

如何解决误差?

参考:延时比指定值更长的原因

如果是应用于弱时效性的场景,可以忽略延时误差。如短信验证码重新发送倒计时。

如果是秒杀、抢购等强时效性的场景,通过服务器时间校对比较靠谱。可以监听页面可见或隐藏事件、固定间隔时间等方式获取最新的服务器时间,然后更新倒计时。

const countdown = new Countdown({
  // ...
});

document.addEventListener('visibilityChange', function () {
  if (!document.hidden) {
    // 获取服务器时间
    // ...
    countdown.updateOptions({
      time: finalTime - serviceTime
    });
    countdown.restart();
  }
});

setInterval(() => {
  // 获取服务器时间
  // ...
  countdown.updateOptions({
    time: finalTime - serviceTime
  });
  countdown.restart();
}, 60 * 1000);