@tempots/std
interval() function
Executes a function repeatedly at a fixed interval.
Signature:
interval: (fn: () => void, ms: number) => () => void
Parameters
Parameter |
Type |
Description |
---|---|---|
fn |
() => void |
The function to execute periodically. |
ms |
number |
The number of milliseconds between each execution. |
Returns: () => void
A function that, when called, will cancel the interval and stop future executions.
Example
// Execute a function every 2 seconds
const stop = interval(() => console.log('Tick'), 2000);
// Stop the interval after some time
setTimeout(() => stop(), 10000);