useInterval

A React Hook that create a interval counter.

Demo

Source
Intervals:
0
Count:
0
Active:
false

Usage

See API for more details.

Source

Click links below to view source on GitHub.

API

const count = useInterval(interval) const { count, reset, ...controls } = useInterval(interval, { controls: true })

Interval

A number that represents the interval in milliseconds.

Options

export type UseIntervalOptions<Controls extends boolean> = { /** * Expose more controls * * @defaultValue false */ controls?: Controls /** * Execute the update immediately on calling * * @defaultValue true */ immediate?: boolean /** * Callback on every interval */ callback?: (count: number) => void }

Returns

Retuerns contain Pausable instance that can be paused, resumed.

See Pausable for more details.

export interface UseIntervalAction extends UseCounterReturnsAction, Pausable { /** * Reset the count */ reset: (count?: number) => void } export type UseIntervalWithControlsReturns = [ /** * Current count */ count: number, /** * Counter Actions */ UseIntervalAction, ] export type UseIntervalReturns<Controls extends boolean> = Controls extends true ? UseIntervalWithControlsReturns : number