useIntervalFn
Reactive setInterval wrapper with pause/resume control
Execute a function repeatedly at a given interval with reactive isActive state and pause/resume control.
Returns a Pausable — isActive is a ReadonlyObservable<boolean>.
Interval Function
Paused
Cycles through greetings every 500ms.
import { import useIntervalFn
useIntervalFn } from "@usels/web";
function function Component(): void
Component() { const { const isActive: any
isActive, const pause: any
pause, const resume: any
resume } = import useIntervalFn
useIntervalFn(() => { any
console.any
log("tick"); }, 1000);
// isActive.get() === true while running // pause() / resume() to control execution}import { createIntervalFn, observable } from "@usels/web";
function Component() { "use scope" const { isActive$, pause, resume } = createIntervalFn(() => { console.log("tick"); }, observable(1000));
// isActive$.get() === true while running // pause() / resume() to control execution}immediateCallback
Section titled “immediateCallback”import { useIntervalFn } from "@usels/web";
function Component() { useIntervalFn(() => fetchData(), 5000, { immediateCallback: true }); // calls fetchData() immediately on resume, then every 5s}import { createIntervalFn, observable } from "@usels/web";
function Component() { "use scope" createIntervalFn(() => fetchData(), observable(5000), { immediateCallback: true }); // calls fetchData() immediately on resume, then every 5s}Reactive interval
Section titled “Reactive interval”import { observable, useIntervalFn } from "@usels/web";
function Component() { const ms$ = observable(1000); useIntervalFn(() => {}, ms$); // ms$.set(500) → automatically restarts with new interval}import { createIntervalFn, observable } from "@usels/web";
function Component() { "use scope" const ms$ = observable(1000); createIntervalFn(() => {}, ms$); // ms$.set(500) → automatically restarts with new interval}interval must be a positive number. 0 or negative values are passed directly to setInterval, which the browser clamps to ~4ms. Use pause() to stop execution instead of setting interval to 0.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
cb | AnyFn | - |
interval | MaybeObservable<number> | undefined (optional) | - |
options | IntervalFnOptions | undefined (optional) | - |
IntervalFnOptions
Section titled “IntervalFnOptions”Returns
Section titled “Returns”Pausable
| Name | Type | Description |
|---|---|---|
isActive$ | ReadonlyObservable<boolean> | - |
pause | Fn | - |
resume | Fn | - |