Giter Club home page Giter Club logo

use-countdown's Introduction

⏳ 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})

Name Required Default Example Description
minutes false 0 1 Countdown minutes.
seconds false 0 30 Countdown seconds.
format false mm:ss mm:ss:SS Format string (reference).
autoStart false false true Whether or not to automatically start the countdown.
onCompleted false undefined function Function 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.

Name Type Example Description
minutes number 1 Remaining minutes.
seconds number 30 Remaining seconds.
formatted string 01:30 Formatted remaining time.
isActive boolean true Indicates that the countdown is active, either running or paused.
isInactive boolean false Indicates that the countdown is inactive, and has finished counting down.
isRunning boolean true Indicates that the countdown is running.
isPaused boolean false Indicates that the countdown is paused.
pause function function Pauses the countdown.
resume function function Resumes the countdown.
reset function function Resets 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

🐛 💻

use-countdown's People

Contributors

allcontributors[bot] avatar bboydflo avatar bgarropy-atlassian avatar bradgarropy avatar msopacua avatar visormatt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

use-countdown's Issues

Add a new feature: allow changing the duration of the countdown

Hello,

I was just wondering if it would be possible to add an ability to change the countdown duration times after useCountdown() has been called.

For example, if you call reset(), the countdown will restart but always for the duration of time specified on the initial useCountdown() call (e.g., 5 seconds). The ability to change the duration of time on a reset() would help me significantly with a project I'm working on.

Thank you for reading this,
SirIsaacNeutron

calling reset() with a time value is causing an error in Next.js (v12)

Hey there @bradgarropy 🤘 First of all, thanks so much for putting this together, it's been lovely to use!

Unfortunately, I just tested out resetting the timer with a specific time object and it keeps throwing this error: RangeError: Invalid time value

image

What's confusing is that the error is targeting the hook line, even though I'm making the reset() call in another place entirely, via:

resendCodeCountdown.reset({ seconds: 30, format: "s 'sec'" })

Additionally here are some details on the versions I'm running this on:

Any ideas why this might not be working as expected? Thanks so much!

Implementing a reset countdown feature

I want to have the possibility to reset the countdown when a button is clicked. I think it could be relevant to restart the countdown without re-rendering the component.

"Cannot find module: 'tslib'"

I'm trying to use this in my Vite + TypeScript + React project. I'm getting this error:

Error: Cannot find module 'tslib'
Require stack:

I found the reference to tslib in the compiled dist:

"use strict";
exports.__esModule = true;
var tslib_1 = require("tslib");
var countdown_1 = tslib_1.__importDefault(require("./countdown"));
exports["default"] = countdown_1["default"];
tslib_1.__exportStar(require("./types"), exports);
//# sourceMappingURL=index.js.map

Looking in package.json in this repo, it appears that tslib is a dev dependency.

Should that be moved to the regular dependencies list, or should the build step be modified somehow to remove the reference to tslib from the compiled distribution?

Countdown overtime

Hi,

Is there anyway to allow the timer keep going even after reaching 0?

So, when the timer hits 0, it starts counting up.

Implementing a way to freeze/stop the timer

I was wondering if it's possible to implement a freeze/stop for the timer. My use case is building a chess timer, and I need to be able to freeze the timer for each side when someone makes a move.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.