useTimeoutFn
Execute a function after a given delay with reactive isPending$ state and manual start/stop control.
Returns a Stoppable — isPending$ is an Observable<boolean> you can subscribe to.
Please wait for 3 seconds
import { import useTimeoutFn
useTimeoutFn } from "@usels/web";
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)Manual start (immediate: false)
Section titled “Manual start (immediate: false)”import { import useTimeoutFn
useTimeoutFn } from "@usels/web";
const { const isPending$: any
isPending$, const start: any
start } = import useTimeoutFn
useTimeoutFn(() => any
console.any
log("done"), 500, { immediate: boolean
immediate: false });
// call start() manually when readyconst start: any
start();Reactive interval (MaybeObservable)
Section titled “Reactive interval (MaybeObservable)”import { import useTimeoutFn
useTimeoutFn } from "@usels/web";import { function observable<T>(): Observable<T | undefined> (+2 overloads)
observable } from "@legendapp/state";
const const delay$: any
delay$ = observable<unknown>(value: Promise<unknown> | (() => unknown) | unknown): any (+2 overloads)
observable(1000);const { const start: any
start } = import useTimeoutFn
useTimeoutFn(() => {}, const delay$: any
delay$);// start() always reads current value of delay$immediateCallback
Section titled “immediateCallback”import { import useTimeoutFn
useTimeoutFn } from "@usels/web";
const { const start: any
start } = import useTimeoutFn
useTimeoutFn((msg: string | undefined
msg?: string) => any
console.any
log("fired", msg: string | undefined
msg), 1000, { immediate: boolean
immediate: false, immediateCallback: boolean
immediateCallback: true,});
const start: any
start("hello");// → cb() called immediately with no args// → cb("hello") called again after 1000msType Declarations
Section titled “Type Declarations”export interface UseTimeoutFnOptions { immediate?: boolean; immediateCallback?: boolean;}export declare function useTimeoutFn<CallbackFn extends AnyFn>(cb: CallbackFn, interval: MaybeObservable<number>, options?: UseTimeoutFnOptions): Stoppable<Parameters<CallbackFn> | []>;Source
Section titled “Source”Contributors
Section titled “Contributors”- tigerwest
Changelog
Section titled “Changelog”a7392ab2026-03-06 - feat(core,browser): add sync strategy hooks (tigerwest)