Documentation Index
Fetch the complete documentation index at: https://mintlify.com/rijvi-mahmud/shaddy/llms.txt
Use this file to discover all available pages before exploring further.
useInterval is a React hook that sets up a recurring interval and executes a callback at the given delay, while also exposing start, stop, reset, and isRunning controls so you can manage the interval lifecycle imperatively. Passing null as the delay pauses the interval without unmounting the component, and the hook automatically clears the interval when the component unmounts — no manual clearInterval needed. It is ideal for countdowns, polling, animations, and any feature that needs repeating behaviour with runtime control.
Installation
Signature
Parameters
The function to execute on each interval tick. The hook stores the latest version in a ref, so you can safely use an inline function or a callback that closes over state — it will always call the most recent version without resetting the timer.
The interval period in milliseconds. Pass
null to pause the interval; the state and controls are preserved and you can resume with start(). Pass 0 for the fastest possible interval (one event-loop tick).Optional configuration.
Return Value
Clears the interval and sets
isRunning to false. Calling stop when the interval is already stopped has no effect.Starts (or restarts) the interval. Has no effect when
delay is null or the interval is already running.Clears the current interval and immediately starts a fresh one (if
autoStart is true and delay is not null). Useful for restarting a countdown from zero.Reactive boolean that is
true while the interval is active and false when it has been stopped or paused.Usage
Countdown Timer
Polling with Manual Start
Notes
- The
callbackref is updated on every render, so you never need to includecallbackin the hook’s dependency array — the latest version is always used. - Passing
nulltodelayis the idiomatic way to pause the interval. The hook callsclearIntervalimmediately andisRunningbecomesfalse. - The interval is automatically cleaned up when the component unmounts, preventing memory leaks and calls to callbacks on unmounted components.
resetstops then conditionally restarts the interval based on theautoStartoption. IfautoStartisfalse,resetonly stops the interval.