Skip to main content
Version: Next (Unreleased)

useDelay

Executes a callback after a configurable delay. Supports both automatic execution on mount and manual trigger mode.

Signature​

// Automatic mode
function useDelay(
callback: () => void,
options?: { delay?: number; manual?: false }
): () => void;

// Manual mode
function useDelay(
callback: () => void,
options: { delay?: number; manual: true }
): () => () => void;

Options​

PropertyTypeDefaultDescription
delaynumber250Milliseconds before the callback fires
manualbooleanfalseWhen true, the hook returns a trigger function instead of auto-executing

Return value​

ModeReturnDescription
manual: false() => voidNo-op (auto-executes on mount)
manual: true() => () => voidCall it to schedule the callback. It returns a function that cancels that run
  • The callback always runs with its latest version, so inline functions are fine.
  • In automatic mode, the timer restarts when delay changes.
  • Pending timers, in both modes, are cleared when the component unmounts.
const run = useDelay(save, { delay: 1000, manual: true });

const cancel = run(); // schedules save() in 1 s
cancel(); // cancels that run

Live example​

Loading playground...