Gathering detailed insights and metrics for react-hook-final-countdown
Gathering detailed insights and metrics for react-hook-final-countdown
Gathering detailed insights and metrics for react-hook-final-countdown
Gathering detailed insights and metrics for react-hook-final-countdown
countdown hook for react functional components
npm install react-hook-final-countdown
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
3 Stars
31 Commits
2 Watchers
1 Branches
1 Contributors
Updated on Jan 17, 2023
Latest Version
2.0.0
Package Id
react-hook-final-countdown@2.0.0
Unpacked Size
15.56 kB
Size
5.36 kB
File Count
5
NPM Version
8.13.2
Node Version
18.6.0
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Provides a hook for React which updates at a regular interval until a target time is reached.
See the examples for other use cases.
1npm install --save react-hook-final-countdown
1const useCountdown = require('react-hook-final-countdown'); 2 3const MyComponent = ({targetTime}) => { 4 const remaining = useCountdown(targetTime, 1000); 5 6 if (remaining >= 0) { 7 return (<div>Seconds remaining: {remaining / 1000}</div>); 8 } 9 return (<div>Countdown ended!</div>); 10};
1const remaining = useCountdown(targetTime, interval); 2 3const time = useTimeInterval(interval[, anchorTime]); 4 5const after = useIsAfter(time); 6 7const before = useIsBefore(time);
All times are in milliseconds. All parameters can be changed dynamically.
useCountdown(targetTime, interval)
Counts down to the given target time.
targetTime
: the timestamp to count down to (milliseconds since the
unix epoch).interval
: number of milliseconds between refreshes; this will
control how often the component is re-rendered, and will be used to
quantise the returned remaining milliseconds.The returned value is -1 after the target time has been reached. Until then, it is the remaining number of milliseconds quantised by the requested interval (rounded down).
For example, for an interval of 800ms, returned values may be:
Positive returned values will always be a multiple of the requested interval (to within numeric precision).
useTimeInterval(interval[, anchorTime])
Provides an infinite timer, updating every interval
milliseconds.
interval
: number of milliseconds between refreshes; this will
control how often the component is re-rendered, and will be used to
quantise the returned time.anchorTime
: a time which can be in the future or the past; this
will control the "phase" of the clock. For example, setting
interval
to 1000 and anchorTime
to 500 will cause an update
every second on the half-second boundary. Defaults to 0.Returns the current timestamp, quantised using interval
.
useIsAfter(targetTime)
A convenience wrapper around useCountdown
. Equivalent to:
1useCountdown(targetTime, POSITIVE_INFINITY) < 0
targetTime
: the timestamp to wait for (milliseconds since the
unix epoch).Returns false until the target time is reached, then true.
useIsBefore(targetTime)
A convenience wrapper around useCountdown
. Equivalent to:
1useCountdown(targetTime, POSITIVE_INFINITY) >= 0
targetTime
: the timestamp to wait for (milliseconds since the
unix epoch).Returns true until the target time is reached, then false.
<TimeProvider scheduler={scheduler}>
An entry point to manipulate the flow of time (e.g. for testing).
1const myScheduler = { 2 getTime: () => Date.now(), 3 schedule: (fn, target) => { 4 const tm = setTimeout(fn, target - Date.now()); 5 return () => clearTimeout(fn); 6 }, 7}; 8 9const MyApp = () => ( 10 <TimeProvider scheduler={myScheduler}> 11 <MyComponent /> 12 </TimeProvider> 13);
By default, a Scheduler
with default parameters is used. When using
custom scheduling, it is recommended that you configure or wrap a
Scheduler
rather than creating a new implementation from scratch
(e.g. see the "half speed" example below).
Note that it is not possible to change the scheduler
parameter; if
you need to change the scheduler dynamically, do so within the
scheduler itself.
new Scheduler(options)
A class which provides scheduling ability, including aggregation of multiple scheduled tasks and throttling when the page is not visible.
1const myScheduler = new Scheduler({ 2 getTime: Date.now, 3 visibleThrottle: 10, 4 hiddenThrottle: 500, 5}); 6 7const MyApp = () => ( 8 <TimeProvider scheduler={myScheduler}> 9 <MyComponent /> 10 </TimeProvider> 11);
options
: configuration options for the scheduler:
getTime
: a function which returns time since an epoch in
milliseconds. Defaults to Date.now
.visibleThrottle
: a minimum delay to apply when the page is
visible (i.e. the browser tab is selected). Defaults to 10ms.hiddenThrottle
: a minimum delay to apply when the page is
not visible (i.e. another browser tab is selected).
Defaults to 500ms to reduce load on the client CPU.
When the page regains focus, all timers will be checked (as
the timers may slip while the window is not visible in many
browsers)If you provide a custom getTime
implementation, note that the
scheduled tasks will still assume that the time will increment in
real-time (i.e. its value will increase by 1000 every second). If
you want to modify the flow of time, you will need to use a wrapper
around schedule
(e.g. see the
"half speed" example below).
This class is designed for use with the TimeProvider
, but can
also be used independently:
1const myScheduler = new Scheduler({
2 getTime: Date.now,
3 visibleThrottle: 10,
4 hiddenThrottle: 500,
5});
6
7const now = myScheduler.getTime();
8const cancel = myScheduler.schedule(myFn, now + 5000);
9// in 5 seconds, myFn will be invoked,
10// or call cancel() to cancel the task
1const {useCountdown} = require('react-hook-final-countdown'); 2 3const Y3K = Date.UTC(3000); 4 5const MyComponent = () => { 6 const remaining = useCountdown(Y3K, 1000); 7 8 if (remaining >= 0) { 9 return (<div>Seconds remaining: {remaining / 1000}</div>); 10 } 11 return (<div>Happy new year!</div>); 12};
1const {useIsAfter} = require('react-hook-final-countdown'); 2 3const MyDelayedButton = ({onClick}) => { 4 const [firstRenderedTime] = useState(Date.now()); 5 const enabled = useIsAfter(firstRenderedTime + 1000); 6 7 return (<button disabled={!enabled} onClick={onClick}>Continue</button>); 8};
1const {useIsAfter} = require('react-hook-final-countdown'); 2 3const MyDelayedButton = ({onClick}) => { 4 const [lastClicked, setLastClicked] = useState(Number.NEGATIVE_INFINITY); 5 const enabled = useIsAfter(lastClicked + 1000); 6 const clickHandler = () => { 7 setLastClicked(Date.now()); 8 onClick(); 9 }; 10 11 return (<button disabled={!enabled} onClick={clickHandler}>Continue</button>); 12};
1const {useTimeInterval} = require('react-hook-final-countdown'); 2 3const MyClock = () => { 4 const time = useTimeInterval(1000); 5 6 return (<span>{new Date(time).toString()}</span>); 7};
1const {Scheduler, TimeProvider} = require('react-hook-final-countdown'); 2 3const scheduler = new Scheduler(); 4const slowMotionScheduler = { 5 getTime: () => scheduler.getTime() * 0.5, 6 schedule: (fn, target) => scheduler.schedule(fn, target * 2), 7}; 8 9const MySlowMotionApp = () => ( 10 <TimeProvider scheduler={slowMotionScheduler}> 11 <MyComponent /> 12 </TimeProvider> 13);
1const {TimeProvider} = require('react-hook-final-countdown'); 2 3const time = Date.UTC(2000); 4const frozenScheduler = { 5 getTime: () => time, // fixed time 6 schedule: (fn, target) => () => null, // no-op 7}; 8 9const MyFrozenApp = () => ( 10 <TimeProvider scheduler={frozenScheduler}> 11 <MyComponent /> 12 </TimeProvider> 13);
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More