use-countdown
Counts down to a target date or from a given duration
Source
Docs
Package
Usage
use-countdown counts down from a given duration (countStart) or to a target date
(targetDate). It returns the remaining time in milliseconds and the same value split into
days, hours, minutes, seconds and milliseconds.
With countStart, the countdown does not start automatically – call start to begin.
Once it reaches 0, calling start again restarts it from countStart:
Count down to a target date
Pass targetDate (Date, timestamp or date string) to count down to a specific moment.
In this mode, the countdown starts automatically:
00
Days
00
Hours
00
Minutes
00
Seconds
Controls
Use start, stop and reset functions to control the countdown. start resumes
a stopped countdown from the current count. Changing countStart while the countdown
is not running resets count to the new value:
10s
Running: no
Interval and sub-second precision
interval option controls how often the countdown updates (1000ms by default). Use a smaller
interval to display milliseconds:
5.000
The remaining time is computed from the end timestamp, so the countdown does not drift when ticks are delayed, for example in background tabs.
onComplete and onTick
onComplete is called once when the countdown reaches 0, onTick is called on every tick with
the remaining number of milliseconds:
API
Options:
targetDate– date to count down to (mutually exclusive withcountStart)countStart– duration in milliseconds to count down frominterval– amount of milliseconds between each tick,1000by defaultautoStart– whether to start automatically,truefortargetDate,falseforcountStartonComplete– called once when the countdown reaches0onTick– called on every tick with the remaining milliseconds
Return object:
count– remaining time in milliseconds, never less than0formatted–countsplit intodays,hours,minutes,secondsandmillisecondsrunning– whether the countdown is currently runningstart– starts or resumes the countdownstop– stops the countdown and keeps the currentcountreset– stops the countdown and resetscounttocountStartor to the time remaining untiltargetDate
Definition
Exported types
UseCountdownOptions, UseCountdownReturnValue and UseCountdownFormatted types are exported from
the @mantine/hooks package, you can import them in your application: