defer

Commons
stable

Schedules a callback after a given timeout (ms) using window.setTimeout in the browser. When called during SSR or with timeout = 0, the callback runs synchronously. Returns a cancel function.

API

Signature

Full TypeScript signature sourced from the util file.

typescript

API

Parameters

Input parameters accepted by this util.

timeout
required
number
Delay in milliseconds before the callback is invoked. Pass 0 to run synchronously.
cb
required
() => void
The function to call after the timeout.

API

Return value

The value returned by this util.

A cancel function. Calling it clears the pending timeout so the callback is never invoked.

Usage

Examples

Ready-to-paste usage snippets.

Delayed execution with cancel

typescript