@bradgarropy/use-countdown

中文文档
2.0.1发布于 2023/10/12MITTypeScriptCommonJS近 30 天下载 9145

⏳ useCountdown hook

原始使用文档
文档目录6 节
  1. 📦 Installation
  2. 🥑 Usage
  3. 📖 API Reference
  4. useCountdown({minutes, seconds})
  5. ❔ Questions
  6. ✨ Contributors

⏳ useCountdown hook

version downloads size github actions codecov typescript contributing contributors discord

React hook countdown timer. As seen on my Twitch streams.

next link

📦 Installation

This package is hosted on npm.

npm install @bradgarropy/use-countdown

🥑 Usage

In any React component, import useCountdown, then call it like any other hook. The returned countdown value will update every second with the remaining time.

import useCountdown from "@bradgarropy/use-countdown"

const App = () => {
    const countdown = useCountdown({
        minutes: 1,
        seconds: 30,
        format: "mm:ss",
        autoStart: true,
        onCompleted: () => console.log("onCompleted"),
    })

    console.log(countdown)

    // {minutes: 1, seconds: 30, formatted: "01:30", ...}
    // {minutes: 1, seconds: 29, formatted: "01:29", ...}
    // {minutes: 1, seconds: 28, formatted: "01:28", ...}
    // ...
    // {minutes: 0, seconds: 0, formatted: "00:00", ...}
    // onCompleted()
}

📖 API Reference

useCountdown({minutes, seconds})

NameRequiredDefaultExampleDescription
minutesfalse01Countdown minutes.
secondsfalse030Countdown seconds.
formatfalsemm:ssmm:ss:SSFormat string (reference).
autoStartfalsefalsetrueWhether or not to automatically start the countdown.
onCompletedfalseundefinedfunctionFunction to call when countdown completes.

Starts a countdown timer based on the number of minutes and seconds provided. The returned countdown object updates once per second and stops when the timer hits zero.

The format parameter is a date-fns format string.

If provided, the onCompleted function will be called when the countdown completes.

Here are some examples of how to call useCountdown.

const countdown = useCountdown({
    minutes: 1,
    seconds: 30,
    format: "mm:ss:SS",
    autoStart: true,
    onCompleted: () => console.log("onCompleted"),
})

const countdown = useCountdown({
    minutes: 5,
    onCompleted: () => console.log("onCompleted"),
})

const countdown = useCountdown({seconds: 10, format: "mm:ss:SS"})

The return object is updated every second until the countdown timer ends.

NameTypeExampleDescription
minutesnumber1Remaining minutes.
secondsnumber30Remaining seconds.
formattedstring01:30Formatted remaining time.
isActivebooleantrueIndicates that the countdown is active, either running or paused.
isInactivebooleanfalseIndicates that the countdown is inactive, and has finished counting down.
isRunningbooleantrueIndicates that the countdown is running.
isPausedbooleanfalseIndicates that the countdown is paused.
pausefunctionfunctionPauses the countdown.
resumefunctionfunctionResumes the countdown.
resetfunctionfunctionResets the countdown.

Here is an example of the returned object.

{
    minutes: 1,
    seconds: 30,
    formatted: "01:30",
    isActive: true,
    isInactive: false,
    isRunning: true,
    isPaused: false,
    pause: () => void,
    resume: () => void,
    reset: (time?: Time) => void,
}

❔ Questions

🐛 report bugs by filing issues
📢 provide feedback with issues or on twitter
🙋🏼‍♂️ use my ama or twitter to ask any other questions

✨ Contributors

Brad Garropy
Brad Garropy

💻 📖 ⚠️ 🚇
Matthew Scholta
Matthew Scholta

💻 📖
James Q Quick
James Q Quick

🤔 📓
Steven Hofheins
Steven Hofheins

📝 ✅
Jack Reiker
Jack Reiker

🤔 📓
Mehdi Makhloufi
Mehdi Makhloufi

🤔 📓
Daniel Badir
Daniel Badir

🐛 🤔 📓
Florin Cosmin
Florin Cosmin

💻
Nick DiMatteo
Nick DiMatteo

🐛
Jimmy Longley
Jimmy Longley

🤔
Nuno Fonseca
Nuno Fonseca

🤔
Nathan Arthur
Nathan Arthur

🐛
msopacua
msopacua

🐛 💻

版本与发布记录

@bradgarropy/use-countdown 共发布 24 个版本,首次发布于 2021/01/27;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.1latest2023/10/123 年前
2.0.02023/07/193 年前
2.0.0-alpha.02023/07/193 年前
1.5.22022/11/173 年前
1.5.12022/06/254 年前
1.5.02022/06/224 年前

关于 @bradgarropy/use-countdown 的常见问题

@bradgarropy/use-countdown 是什么?

⏳ useCountdown hook

如何安装 @bradgarropy/use-countdown?

在项目目录运行 npm install @bradgarropy/use-countdown,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=16)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@bradgarropy/use-countdown 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bradgarropy/use-countdown 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bradgarropy/use-countdown 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=10。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@bradgarropy/use-countdown 可以商用吗?

@bradgarropy/use-countdown 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bradgarropy/use-countdown 的下载量有多少?

根据 npm 官方下载统计,@bradgarropy/use-countdown 在 2026/10/09 – 2026/11/07 这 30 天内被下载 9,145 次,日均约 305 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bradgarropy/use-countdown 还在维护吗?

最新版本 2.0.1 发布于 2023/10/12(3 年前);至今共发布 24 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/01/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。