useIntervalWhen

Create dynamic timers that can be started, paused, or resumed with useIntervalWhen.

Install:

Note: This hook depends on React’s experimental useEffectEvent.

npm i @uidotdev/usehooks@experimental react@experimental react-dom@experimental

Description:

The useIntervalWhen hook is useful for creating an interval timer that can be controlled based on certain conditions. It allows you to specify a callback function to be executed at a regular interval specified by the ms parameter. Additionally, you can choose whether the interval should start immediately or wait for a trigger by setting the startImmediately parameter. This hook is particularly handy when you need to create dynamic timers that can be started, paused, or resumed based on specific conditions.

Parameters

NameTypeDescription
cbfunctionThe callback function to be executed at the specified interval when the condition is met.
optionsobjectAn object containing the following options.
options.msnumberThe interval duration in milliseconds.
options.whenbooleanThe condition that determines whether the interval should be active (true) or paused (false).
options.startImmediatelyboolean(Optional) Whether to start the interval immediately when the condition is met. Default is false.

Return Value

TypeDescription
functionA function to clear the interval and pause the execution.

Demo:

Example:

More Hooks:

Sort: