bare-timers
Native timers for Javascript
bare-timers — Native timers for Javascript. It is a native addon and requires Bare >=1.7.0.
Mirrors the Node.js timers module.
npm i bare-timersUsage
const { setTimeout, clearTimeout } = require('bare-timers')API
Functions
setTimeout
setTimeout<T extends unknown[]>(callback: (...args: T) => unknown, delay: number, ...args: T): TimeoutSchedule execution once after delay milliseconds, clamped to a minimum of 1ms.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
callback | (...args: T) => unknown | — | The function to run after the delay. |
delay | number | — | Milliseconds to wait before running; clamped to a minimum of 1. |
args | T | — | Additional arguments passed to callback. |
clearTimeout(timer: Timeout): void
Source
Cancel a pending timeout, preventing it from firing.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
timer | Timeout | — | The timeout handle to cancel. |
setInterval
setInterval<T extends unknown[]>(callback: (...args: T) => unknown, delay: number, ...args: T): TimeoutSchedule repeated execution every delay milliseconds, clamped to a minimum of 1ms.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
callback | (...args: T) => unknown | — | The function to run on each interval. |
delay | number | — | Milliseconds between runs; clamped to a minimum of 1. |
args | T | — | Additional arguments passed to callback. |
clearInterval(timer: Timeout): void
Source
Cancel a pending interval, preventing further firings.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
timer | Timeout | — | The interval handle to cancel. |
setImmediate<T extends unknown[]>(callback: (...args: T) => unknown, ...args: T): Immediate
Source
Schedule execution once at the end of the current event loop iteration.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
callback | (...args: T) => unknown | — | The function to run at the end of the current event loop iteration. |
args | T | — | Additional arguments passed to callback. |
clearImmediate(immediate: Immediate): void
Source
Cancel a pending immediate, preventing it from firing.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
immediate | Immediate | — | The immediate handle to cancel. |
Types
Task
interface Task {
ref(): this
unref(): this
hasRef(): boolean
}The base handle shared by Timeout and Immediate, controlling whether it keeps the event loop alive.
Timeout
interface Timeout extends Task {
refresh(): this
}The handle returned by setTimeout and setInterval.
Immediate
interface Immediate extends Task {}The handle returned by setImmediate.
bare-timers/promises
Functions
setTimeout<T>(delay?: number, value?: T, options?: TimeoutOptions): Promise<T>
Source
Schedule execution once after delay milliseconds, clamped to a minimum of 1ms.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
delay? | number | — | Milliseconds to wait before running; clamped to a minimum of 1. |
value? | T | — | The value the returned promise resolves with. |
options? | TimeoutOptions | — | Options; ref defaults to true (set false to unref), and signal may be an AbortSignal that cancels the timer. |
setInterval<T>(delay?: number, value?: T, options?: TimeoutOptions): AsyncGenerator<T>
Source
Schedule repeated execution every delay milliseconds, clamped to a minimum of 1ms.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
delay? | number | — | Milliseconds between runs; clamped to a minimum of 1. |
value? | T | — | The value yielded on each iteration. |
options? | TimeoutOptions | — | Options; ref defaults to true (set false to unref), and signal may be an AbortSignal that cancels the timer. |
setImmediate<T>(value?: T, options?: ImmediateOptions): Promsie<T>
Source
Schedule execution once at the end of the current event loop iteration.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
value? | T | — | The value the returned promise resolves with. |
options? | ImmediateOptions | — | Options; ref defaults to true (set false to unref), and signal may be an AbortSignal that cancels the timer. |
Types
TaskOptions
interface TaskOptions {
ref?: boolean
signal?: AbortSignal
}Shared options for the bare-timers/promises scheduling functions.
TimeoutOptions
interface TimeoutOptions extends TaskOptions {}Options for bare-timers/promises' setTimeout and setInterval.
ImmediateOptions
interface ImmediateOptions extends TaskOptions {}Options for bare-timers/promises' setImmediate.
See also
- Bare modules — the full
bare-*catalog. - Bare runtime API — the runtime these modules extend.