Skip to content
0

katanakit-js / useInterval

Function: useInterval() ​

ts
function useInterval(
   callback, 
   ms, 
   immediate?
): IntervalControl;

Defined in: src/core/services/timing.service.ts:72

Repeatedly executes a callback at ms intervals with pause/resume/stop controls. Uses recursive setTimeout internally to avoid overlap.

Parameters ​

ParameterTypeDefault valueDescription
callback() => void | Promise<void>undefinedFunction executed on each tick.
msnumberundefinedInterval in milliseconds.
immediatebooleanfalseIf true, fires the callback immediately before the first interval (defaults to false).

Returns ​

IntervalControl

An IntervalControl with pause, resume, stop, and isRunning.

Example ​

ts
const timer = useInterval(() => console.log("tick"), 1000);
setTimeout(() => timer.pause(), 3000);
setTimeout(() => timer.resume(), 5000);
setTimeout(() => timer.stop(), 10000);

Released under the MIT License.