useRandomInterval

Execute a callback function at a random interval with useRandomInterval.

Install:

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

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

Description:

The useRandomInterval hook is useful for executing a callback function at random intervals within a specified range. It provides a way to create an interval that varies dynamically based on minimum and maximum delay values. By using this hook, you can easily implement features such as periodic data updates, animations, or any other functionality that requires dynamic and random timing. The hook manages the interval internally, ensuring that it is cleared when the component unmounts or when the delay values change.

Parameters

NameTypeDescription
cbfunctionThe callback function to be executed at random intervals.
optionsobjectAn object containing the following options.
options.minDelaynumberThe minimum delay in milliseconds between each callback invocation.
options.maxDelaynumberThe maximum delay in milliseconds between each callback invocation.

Return Value

TypeDescription
functionA function to clear the timeout and stop the random interval execution.

Demo:

Example:

More Hooks:

Sort: