AsyncClient Component
useInterval
Overview
useInterval makes an interval a function of React state: pass a delay to run it or null to pause it.
Signature
ts
function useInterval(
callback: () => void,
delay: number | null,
options?: { onError?: (error: unknown) => void },
): void;Parameters
callback follows the latest commit without recreating the timer. delay is clamped to zero and null pauses scheduling. onError observes callback failures.
Returns
Nothing. Control the interval by changing delay.
Behavior
Changing delay replaces the native interval. Cleanup clears it on dependency changes and unmount. If a callback throws, the interval is stopped before onError runs and the original error is rethrown.
SSR / RSC
Intervals start only in client effects, never during SSR. Call it in a Client Component.
Example
Composition
Use useToggle to control the running state or useOnline to pause polling while offline.