node:timers
Node-compatible callback and promise timer utilities (node:timers).
Available in vjs only (not available in vjs-lite). Enabled by default
unless --no-node-compat is passed. Never installed on globalThis — import
explicitly from "node:timers".
import ... from "node:timers"
node-compat (enabled by default in vjs; absent in vjs-lite; disable with --no-node-compat)Example
Section titled “Example”import { setTimeout, clearTimeout } from "node:timers";
const handle = setTimeout(() => { console.log("fired");}, 10);clearTimeout(handle);setTimeoutvjs onlyv0.0.0-alpha.2callback after delay milliseconds.clearTimeoutvjs onlyv0.0.0-alpha.2Timeout scheduled by setTimeout.setIntervalvjs onlyv0.0.0-alpha.2callback every delay milliseconds.clearIntervalvjs onlyv0.0.0-alpha.2Timeout scheduled by setInterval.setImmediatevjs onlyv0.0.0-alpha.2callback after the current I/O turn.clearImmediatevjs onlyv0.0.0-alpha.2Immediate scheduled by setImmediate.AbortSignalLikevjs onlyv0.0.0-alpha.2timers.promises helpers.TimerOptionsvjs onlyv0.0.0-alpha.2Timeoutvjs onlyv0.0.0-alpha.2setTimeout and setInterval.Immediatevjs onlyv0.0.0-alpha.2setImmediate.TimersSchedulervjs onlyv0.0.0-alpha.2timers.promises.scheduler.TimersPromisesvjs onlyv0.0.0-alpha.2timers.promises (node:timers/promises).Functions
Section titled “Functions”setTimeout
Section titled “setTimeout”export function setTimeout<TArgs extends any[]>(
callback: (...args: TArgs) => void,
delay?: number,
...args: TArgs
): Timeout;
Schedule one-time execution of callback after delay milliseconds.
| Name | Type | Description |
|---|---|---|
callback | (...args: TArgs) => void | Function invoked when the timer expires. |
delay optional | number | Delay in milliseconds (default 1). |
...args | TArgs | Additional arguments forwarded to callback. |
Timeout — Timeout handle for cancellation or ref management.clearTimeout
Section titled “clearTimeout”export function clearTimeout(id: Timeout | string | number | undefined): void;
Cancel a Timeout scheduled by setTimeout.
| Name | Type | Description |
|---|---|---|
id | Timeout | string | number | undefined | Timeout handle or numeric timer ID to cancel. |
voidsetInterval
Section titled “setInterval”export function setInterval<TArgs extends any[]>(
callback: (...args: TArgs) => void,
delay?: number,
...args: TArgs
): Timeout;
Schedule repeated execution of callback every delay milliseconds.
| Name | Type | Description |
|---|---|---|
callback | (...args: TArgs) => void | Function invoked on each interval tick. |
delay optional | number | Interval duration in milliseconds (default 1). |
...args | TArgs | Additional arguments forwarded to callback. |
Timeout — Timeout handle for cancellation or ref management.clearInterval
Section titled “clearInterval”export function clearInterval(id: Timeout | string | number | undefined): void;
Cancel a repeating Timeout scheduled by setInterval.
| Name | Type | Description |
|---|---|---|
id | Timeout | string | number | undefined | Timeout handle or numeric timer ID to cancel. |
voidsetImmediate
Section titled “setImmediate”export function setImmediate<TArgs extends any[]>(
callback: (...args: TArgs) => void,
...args: TArgs
): Immediate;
Schedule immediate execution of callback after the current I/O turn.
| Name | Type | Description |
|---|---|---|
callback | (...args: TArgs) => void | Function invoked on the immediate turn. |
...args | TArgs | Additional arguments forwarded to callback. |
Immediate — Immediate handle for cancellation or ref management.clearImmediate
Section titled “clearImmediate”export function clearImmediate(id: Immediate | undefined): void;
Cancel an Immediate scheduled by setImmediate.
| Name | Type | Description |
|---|---|---|
id | Immediate | undefined | Immediate handle to cancel. |
voidInterfaces
Section titled “Interfaces”AbortSignalLike
Section titled “AbortSignalLike”export interface AbortSignalLike {
readonly aborted: boolean;
readonly reason?: unknown;
addEventListener(type: string, listener: (...args: any[]) => void, options?: { once?: boolean }): void;
removeEventListener?(type: string, listener: (...args: any[]) => void): void;
}
Minimal abort-signal contract accepted by timers.promises helpers.
| Property | Type | Description | Since |
|---|---|---|---|
aborted readonly | boolean | Whether the signal has already been aborted. | v0.0.0-alpha.2 |
reason readonly optional | unknown | Optional abort reason value. | v0.0.0-alpha.2 |
AbortSignalLike.addEventListener
Section titled “AbortSignalLike.addEventListener”addEventListener(type: string, listener: (...args: any[]) => void, options?: { once?: boolean }): void;
Register an "abort" listener.
| Name | Type | Description |
|---|---|---|
type | string | Event type ("abort"). |
listener | (...args: any[]) => void | Callback invoked on abort. |
options optional | { once?: boolean } | Optional listener options. |
voidAbortSignalLike.removeEventListener
Section titled “AbortSignalLike.removeEventListener”removeEventListener?(type: string, listener: (...args: any[]) => void): void;
Unregister an "abort" listener.
| Name | Type | Description |
|---|---|---|
type | string | Event type ("abort"). |
listener | (...args: any[]) => void | Callback to remove. |
voidTimerOptions
Section titled “TimerOptions”export interface TimerOptions {
signal?: AbortSignalLike;
ref?: boolean;
}
Options accepted by promise-based timer functions.
| Property | Type | Description | Since |
|---|---|---|---|
signal optional | AbortSignalLike | Abort signal used to cancel the pending timer. | v0.0.0-alpha.2 |
ref optional | boolean | Keep the event loop active while the timer is pending (default true). | v0.0.0-alpha.2 |
Timeout
Section titled “Timeout”export interface Timeout {
ref(): this;
unref(): this;
hasRef(): boolean;
refresh(): this;
[Symbol.dispose](): void;
}
Timer handle returned by setTimeout and setInterval.
Timeout.ref
Section titled “Timeout.ref”ref(): this;
Keep the event loop active while this timer remains scheduled.
this — this for chaining.Timeout.unref
Section titled “Timeout.unref”unref(): this;
Allow the event loop to exit if this timer is the only pending work.
this — this for chaining.Timeout.hasRef
Section titled “Timeout.hasRef”hasRef(): boolean;
Return true if this timer currently keeps the event loop alive.
boolean — true if ref'd, otherwise false.Timeout.refresh
Section titled “Timeout.refresh”refresh(): this;
Reset the timer’s start time to now and reschedule it with the original delay.
this — this for chaining.Timeout.[Symbol.dispose]
Section titled “Timeout.[Symbol.dispose]”[Symbol.dispose](): void;
Cancel the timer when disposed via using.
voidImmediate
Section titled “Immediate”export interface Immediate {
ref(): this;
unref(): this;
hasRef(): boolean;
[Symbol.dispose](): void;
}
Handle returned by setImmediate.
Immediate.ref
Section titled “Immediate.ref”ref(): this;
Keep the event loop active while this immediate remains scheduled.
this — this for chaining.Immediate.unref
Section titled “Immediate.unref”unref(): this;
Allow the event loop to exit if this immediate is the only pending work.
this — this for chaining.Immediate.hasRef
Section titled “Immediate.hasRef”hasRef(): boolean;
Return true if this immediate currently keeps the event loop alive.
boolean — true if ref'd, otherwise false.Immediate.[Symbol.dispose]
Section titled “Immediate.[Symbol.dispose]”[Symbol.dispose](): void;
Cancel the immediate when disposed via using.
voidTimersScheduler
Section titled “TimersScheduler”export interface TimersScheduler {
wait(delay?: number, options?: { signal?: AbortSignalLike }): Promise<void>;
yield(): Promise<void>;
}
Scheduler utilities exposed on timers.promises.scheduler.
TimersScheduler.wait
Section titled “TimersScheduler.wait”wait(delay?: number, options?: { signal?: AbortSignalLike }): Promise<void>;
Resolve after delay milliseconds.
| Name | Type | Description |
|---|---|---|
delay optional | number | Wait duration in milliseconds (default 0). |
options optional | { signal?: AbortSignalLike } | Optional abort signal configuration. |
Promise<void> — Promise resolving after delay ms.TimersScheduler.yield
Section titled “TimersScheduler.yield”yield(): Promise<void>;
Yield control to the event loop and resolve on a subsequent turn.
Promise<void> — Promise resolving after yielding.TimersPromises
Section titled “TimersPromises”export interface TimersPromises {
setTimeout<T = void>(delay?: number, value?: T, options?: TimerOptions): Promise<T>;
setInterval<T = void>(
delay?: number,
value?: T,
options?: TimerOptions,
): AsyncIterableIterator<T>;
setImmediate<T = void>(value?: T, options?: TimerOptions): Promise<T>;
readonly scheduler: TimersScheduler;
}
Promise-based timer API exposed as timers.promises (node:timers/promises).
| Property | Type | Description | Since |
|---|---|---|---|
scheduler readonly | TimersScheduler | Event-loop scheduling helpers (wait, yield). | v0.0.0-alpha.2 |
TimersPromises.setTimeout
Section titled “TimersPromises.setTimeout”setTimeout<T = void>(delay?: number, value?: T, options?: TimerOptions): Promise<T>;
Return a Promise that fulfills with value after delay milliseconds.
| Name | Type | Description |
|---|---|---|
delay optional | number | Delay in milliseconds (default 1). |
value optional | T | Optional fulfillment value. |
options optional | TimerOptions | Optional abort signal and ref settings. |
Promise<T> — Promise resolving to value.TimersPromises.setInterval
Section titled “TimersPromises.setInterval”setInterval<T = void>(
delay?: number,
value?: T,
options?: TimerOptions,
): AsyncIterableIterator<T>;
Return an async iterator yielding value every delay milliseconds.
| Name | Type | Description |
|---|---|---|
delay optional | number | Interval duration in milliseconds (default 1). |
value optional | T | Optional yielded value. |
options optional | TimerOptions | Optional abort signal and ref settings. |
AsyncIterableIterator<T> — Async iterator yielding value on each tick.TimersPromises.setImmediate
Section titled “TimersPromises.setImmediate”setImmediate<T = void>(value?: T, options?: TimerOptions): Promise<T>;
Return a Promise that fulfills with value after the current I/O turn.
| Name | Type | Description |
|---|---|---|
value optional | T | Optional fulfillment value. |
options optional | TimerOptions | Optional abort signal and ref settings. |
Promise<T> — Promise resolving to value.Constants
Section titled “Constants”promises
Section titled “promises”export const promises: TimersPromises;
Promise-based timer API (node:timers/promises).
Default Export
Section titled “Default Export”default export
Section titled “default export”const _default: {
/** Schedule a one-time timer. @since 0.0.0-alpha.2 */
setTimeout: typeof setTimeout;
/** Cancel a one-time timer. @since 0.0.0-alpha.2 */
clearTimeout: typeof clearTimeout;
/** Schedule a repeating interval timer. @since 0.0.0-alpha.2 */
setInterval: typeof setInterval;
/** Cancel a repeating interval timer. @since 0.0.0-alpha.2 */
clearInterval: typeof clearInterval;
/** Schedule an immediate callback. @since 0.0.0-alpha.2 */
setImmediate: typeof setImmediate;
/** Cancel an immediate callback. @since 0.0.0-alpha.2 */
clearImmediate: typeof clearImmediate;
/** Promise-based timer API (`node:timers/promises`). @since 0.0.0-alpha.2 */
readonly promises: TimersPromises;
};
export default _default;
Default export object providing all node:timers helpers and promises.
