Skip to content
Timer

useTimeoutFn

Reactive wrapper for setTimeout with start/stop control

Execute a function after a given delay with reactive isPending$ state and manual start/stop control. Returns a StoppableisPending$ is an Observable<boolean> you can subscribe to.

Timeout Function
Fired

Fires a callback after 3 seconds.

Please wait for 3 seconds
import {
import useTimeoutFn
useTimeoutFn
} from "@usels/web";
function
function Component(): void
Component
() {
const {
const isPending$: any
isPending$
,
const start: any
start
,
const stop: any
stop
} =
import useTimeoutFn
useTimeoutFn
(() => {
any
console
.
any
log
("fired!");
}, 1000);
// isPending$.get() === true while waiting
// stop() cancels the pending timeout
// start() restarts the timer (resets if already pending)
}
import { useTimeoutFn } from "@usels/web";
function Component() {
const { isPending$, start } = useTimeoutFn(() => console.log("done"), 500, {
immediate: false,
});
// call start() manually when ready
}
import { observable, useTimeoutFn } from "@usels/web";
function Component() {
const delay$ = observable(1000);
const { start } = useTimeoutFn(() => {}, delay$);
// start() always reads current value of delay$
}
import { useTimeoutFn } from "@usels/web";
function Component() {
const { start } = useTimeoutFn((msg?: string) => console.log("fired", msg), 1000, {
immediate: false,
immediateCallback: true,
});
start("hello");
// → cb() called immediately with no args
// → cb("hello") called again after 1000ms
}
ParameterTypeDescription
cbAnyFn- Callback to invoke after the delay.
interval$MaybeObservable<number>- Observable that returns the delay in ms (read at each start() call).
optionsTimeoutFnOptions | undefined (optional)- Mount-time-only options.
OptionTypeDefaultDescription
immediatebooleantrueIf true, calls start() immediately.
immediateCallbackbooleanfalseIf true, calls cb() immediately (no args) when start() is called, then again after the delay with the original args.

Stoppable<any[]>

NameTypeDescription
isPending$ObservableBoolean<boolean>-
stopFn-
start(...args: any[]) => void-

View on GitHub