useTimeoutFn#

A restartable one-shot timer: cb fires once, ms (default 1000) after the latest start(). Cross-platform — import from @sigx/use.

TypeScript
import { signal } from 'sigx';
import { useTimeoutFn } from '@sigx/use';

const toast = signal<string | null>(null);
const { start, isPending } = useTimeoutFn(() => (toast.value = null), 3000);

toast.value = 'Saved';
start(); // clear the toast 3s from now

Calling start() again replaces any pending run — the delay restarts from the latest call. Arguments passed to start() flow through to cb:

TypeScript
const { start } = useTimeoutFn((id: number) => dismiss(id), 3000);
start(42); // cb receives 42 when it fires

The timer is auto-cancelled when the owning scope disposes (component setup or the innermost effectScope()).

Signature#

TypeScript
function useTimeoutFn<A extends unknown[] = []>(
    cb: (...args: A) => void,
    ms?: MaybeSignal<number>,
    options?: UseTimeoutFnOptions,
): UseTimeoutFnReturn<A>;

Options#

OptionTypeDefaultDescription
immediatebooleantrueArm the timer on creation.

Returns#

FieldTypeDescription
isPendingReadSignal<boolean>True while a run is armed and hasn't fired.
start(...args: A) => void(Re)arm the timer, replacing any pending run. Arguments pass through to cb.
stop() => voidCancel the pending run, if any.

Platform: everywhere (setTimeout).

See also#