LogoPear Docs

bare-timers

Native timers for Javascript

stable

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-timers

Usage

const { setTimeout, clearTimeout } = require('bare-timers')

API

Functions

setTimeout

setTimeout<T extends unknown[]>(callback: (...args: T) => unknown, delay: number, ...args: T): Timeout
Source

Schedule execution once after delay milliseconds, clamped to a minimum of 1ms.

Parameters

ParameterTypeDefaultDescription
callback(...args: T) => unknownThe function to run after the delay.
delaynumberMilliseconds to wait before running; clamped to a minimum of 1.
argsTAdditional arguments passed to callback.

clearTimeout(timer: Timeout): void

Source

Cancel a pending timeout, preventing it from firing.

Parameters

ParameterTypeDefaultDescription
timerTimeoutThe timeout handle to cancel.

setInterval

setInterval<T extends unknown[]>(callback: (...args: T) => unknown, delay: number, ...args: T): Timeout
Source

Schedule repeated execution every delay milliseconds, clamped to a minimum of 1ms.

Parameters

ParameterTypeDefaultDescription
callback(...args: T) => unknownThe function to run on each interval.
delaynumberMilliseconds between runs; clamped to a minimum of 1.
argsTAdditional arguments passed to callback.

clearInterval(timer: Timeout): void

Source

Cancel a pending interval, preventing further firings.

Parameters

ParameterTypeDefaultDescription
timerTimeoutThe 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

ParameterTypeDefaultDescription
callback(...args: T) => unknownThe function to run at the end of the current event loop iteration.
argsTAdditional arguments passed to callback.

clearImmediate(immediate: Immediate): void

Source

Cancel a pending immediate, preventing it from firing.

Parameters

ParameterTypeDefaultDescription
immediateImmediateThe immediate handle to cancel.

Types

Task

interface Task {
  ref(): this
  unref(): this
  hasRef(): boolean
}
Source

The base handle shared by Timeout and Immediate, controlling whether it keeps the event loop alive.

Timeout

interface Timeout extends Task {
  refresh(): this
}
Source

The handle returned by setTimeout and setInterval.

Immediate

interface Immediate extends Task {}
Source

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

ParameterTypeDefaultDescription
delay?numberMilliseconds to wait before running; clamped to a minimum of 1.
value?TThe value the returned promise resolves with.
options?TimeoutOptionsOptions; 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

ParameterTypeDefaultDescription
delay?numberMilliseconds between runs; clamped to a minimum of 1.
value?TThe value yielded on each iteration.
options?TimeoutOptionsOptions; 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

ParameterTypeDefaultDescription
value?TThe value the returned promise resolves with.
options?ImmediateOptionsOptions; 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
}
Source

Shared options for the bare-timers/promises scheduling functions.

TimeoutOptions

interface TimeoutOptions extends TaskOptions {}
Source

Options for bare-timers/promises' setTimeout and setInterval.

ImmediateOptions

interface ImmediateOptions extends TaskOptions {}
Source

Options for bare-timers/promises' setImmediate.

See also

On this page