Spec-Zone.ru › Deno 2

timers

The timer module exposes a global API for scheduling functions to be called at some future period of time. Because the timer functions are globals, there is no need to import node:timers to use the API.

The timer functions within Node.js implement a similar API as the timers API provided by Web Browsers but use a different internal implementation that is built around the Node.js Event Loop.

Usage in Deno

import * as mod from "node:timers";

function clearImmediate

Usage in Deno

import { clearImmediate } from "node:timers";
clearImmediate(immediate: Immediate | undefined): void

Cancels an Immediate object created by setImmediate().

Parameters

immediate: Immediate | undefined

An Immediate object as returned by setImmediate().

Return Type

void

function clearInterval

Usage in Deno

import { clearInterval } from "node:timers";
clearInterval(timeout:
Timeout
| string
| number
| undefined
): void

Cancels a Timeout object created by setInterval().

Parameters

timeout:
Timeout
| string
| number
| undefined

A Timeout object as returned by setInterval() or the primitive of the Timeout object as a string or a number.

Return Type

void

function clearTimeout

Usage in Deno

import { clearTimeout } from "node:timers";
clearTimeout(timeout:
Timeout
| string
| number
| undefined
): void

Cancels a Timeout object created by setTimeout().

Parameters

timeout:
Timeout
| string
| number
| undefined

A Timeout object as returned by setTimeout() or the primitive of the Timeout object as a string or a number.

Return Type

void

function promises.setImmediate

Usage in Deno

import { promises } from "node:timers";
const { setImmediate } = promises;
setImmediate<T = void>(
value?: T,
options?: TimerOptions,
): Promise<T>
import {
  setImmediate,
} from 'node:timers/promises';

const res = await setImmediate('result');

console.log(res);  // Prints 'result'

Type Parameters

T = void

Parameters

value: T
optional

A value with which the promise is fulfilled.

options: TimerOptions
optional

Return Type

Promise<T>

function promises.setInterval

Usage in Deno

import { promises } from "node:timers";
const { setInterval } = promises;
setInterval<T = void>(
delay?: number,
value?: T,
options?: TimerOptions,
): AsyncIterator<T>

Returns an async iterator that generates values in an interval of delay ms. If ref is true, you need to call next() of async iterator explicitly or implicitly to keep the event loop alive.

import {
  setInterval,
} from 'node:timers/promises';

const interval = 100;
for await (const startTime of setInterval(interval, Date.now())) {
  const now = Date.now();
  console.log(now);
  if ((now - startTime) > 1000)
    break;
}
console.log(Date.now());

Type Parameters

T = void

Parameters

delay: number
optional

The number of milliseconds to wait between iterations. Default: 1.

value: T
optional

A value with which the iterator returns.

options: TimerOptions
optional

Return Type

AsyncIterator<T>

function promises.setTimeout

Usage in Deno

import { promises } from "node:timers";
const { setTimeout } = promises;
setTimeout<T = void>(
delay?: number,
value?: T,
options?: TimerOptions,
): Promise<T>
import {
  setTimeout,
} from 'node:timers/promises';

const res = await setTimeout(100, 'result');

console.log(res);  // Prints 'result'

Type Parameters

T = void

Parameters

delay: number
optional

The number of milliseconds to wait before fulfilling the promise. Default: 1.

value: T
optional

A value with which the promise is fulfilled.

options: TimerOptions
optional

Return Type

Promise<T>

function queueMicrotask

Usage in Deno

import { queueMicrotask } from "node:timers";
queueMicrotask(callback: () => void): void

The queueMicrotask() method queues a microtask to invoke callback. If callback throws an exception, the process object 'uncaughtException' event will be emitted.

The microtask queue is managed by V8 and may be used in a similar manner to the process.nextTick() queue, which is managed by Node.js. The process.nextTick() queue is always processed before the microtask queue within each turn of the Node.js event loop.

Parameters

callback: () => void

Function to be queued.

Return Type

void

function setImmediate

Usage in Deno

import { setImmediate } from "node:timers";

Overload 1

setImmediate<TArgs extends any[]>(
callback: (...args: TArgs) => void,
...args: TArgs,
): Immediate

Schedules the "immediate" execution of the callback after I/O events' callbacks.

When multiple calls to setImmediate() are made, the callback functions are queued for execution in the order in which they are created. The entire callback queue is processed every event loop iteration. If an immediate timer is queued from inside an executing callback, that timer will not be triggered until the next event loop iteration.

If callback is not a function, a TypeError will be thrown.

This method has a custom variant for promises that is available using timersPromises.setImmediate().

Type Parameters

TArgs extends any[]

Parameters

callback: (...args: TArgs) => void

The function to call at the end of this turn of the Node.js Event Loop

...args: TArgs

Return Type

Immediate

for use with clearImmediate()

Overload 2

setImmediate(callback: (_: void) => void): Immediate

Parameters

callback: (_: void) => void

Return Type

Immediate

namespace setImmediate

Functions

setImmediate.setImmediate
No documentation available

function setImmediate.setImmediate

Usage in Deno

import { setImmediate } from "node:timers";
const { setImmediate } = setImmediate;
setImmediate<T = void>(
value?: T,
options?: TimerOptions,
): Promise<T>
import {
  setImmediate,
} from 'node:timers/promises';

const res = await setImmediate('result');

console.log(res);  // Prints 'result'

Type Parameters

T = void

Parameters

value: T
optional

A value with which the promise is fulfilled.

options: TimerOptions
optional

Return Type

Promise<T>

function setInterval

Usage in Deno

import { setInterval } from "node:timers";

Overload 1

setInterval<TArgs extends any[]>(
callback: (...args: TArgs) => void,
delay?: number,
...args: TArgs,
): Timeout

Schedules repeated execution of callback every delay milliseconds.

When delay is larger than 2147483647 or less than 1 or NaN, the delay will be set to 1. Non-integer delays are truncated to an integer.

If callback is not a function, a TypeError will be thrown.

This method has a custom variant for promises that is available using timersPromises.setInterval().

Type Parameters

TArgs extends any[]

Parameters

callback: (...args: TArgs) => void

The function to call when the timer elapses.

delay: number
optional

The number of milliseconds to wait before calling the callback. Default: 1.

...args: TArgs

Return Type

Timeout

for use with clearInterval()

Overload 2

setInterval(
callback: (_: void) => void,
delay?: number,
): Timeout

Parameters

callback: (_: void) => void
delay: number
optional

Return Type

Timeout

function setTimeout

Usage in Deno

import { setTimeout } from "node:timers";

Overload 1

setTimeout<TArgs extends any[]>(
callback: (...args: TArgs) => void,
delay?: number,
...args: TArgs,
): Timeout

Schedules execution of a one-time callback after delay milliseconds.

The callback will likely not be invoked in precisely delay milliseconds. Node.js makes no guarantees about the exact timing of when callbacks will fire, nor of their ordering. The callback will be called as close as possible to the time specified.

When delay is larger than 2147483647 or less than 1 or NaN, the delay will be set to 1. Non-integer delays are truncated to an integer.

If callback is not a function, a TypeError will be thrown.

This method has a custom variant for promises that is available using timersPromises.setTimeout().

Type Parameters

TArgs extends any[]

Parameters

callback: (...args: TArgs) => void

The function to call when the timer elapses.

delay: number
optional

The number of milliseconds to wait before calling the callback. Default: 1.

...args: TArgs

Return Type

Timeout

for use with clearTimeout()

Overload 2

setTimeout(
callback: (_: void) => void,
delay?: number,
): Timeout

Parameters

callback: (_: void) => void
delay: number
optional

Return Type

Timeout

namespace setTimeout

Functions

setTimeout.setTimeout
No documentation available

function setTimeout.setTimeout

Usage in Deno

import { setTimeout } from "node:timers";
const { setTimeout } = setTimeout;
setTimeout<T = void>(
delay?: number,
value?: T,
options?: TimerOptions,
): Promise<T>
import {
  setTimeout,
} from 'node:timers/promises';

const res = await setTimeout(100, 'result');

console.log(res);  // Prints 'result'

Type Parameters

T = void

Parameters

delay: number
optional

The number of milliseconds to wait before fulfilling the promise. Default: 1.

value: T
optional

A value with which the promise is fulfilled.

options: TimerOptions
optional

Return Type

Promise<T>

interface Immediate

extends RefCountedDisposable

Usage in Deno

import { type Immediate } from "node:timers";

This object is created internally and is returned from setImmediate(). It can be passed to clearImmediate() in order to cancel the scheduled actions.

By default, when an immediate is scheduled, the Node.js event loop will continue running as long as the immediate is active. The Immediate object returned by setImmediate() exports both immediate.ref() and immediate.unref() functions that can be used to control this default behavior.

Methods

hasRef(): boolean

If true, the Immediate object will keep the Node.js event loop active.

ref(): this

When called, requests that the Node.js event loop not exit so long as the Immediate is active. Calling immediate.ref() multiple times will have no effect.

By default, all Immediate objects are "ref'ed", making it normally unnecessary to call immediate.ref() unless immediate.unref() had been called previously.

unref(): this

When called, the active Immediate object will not require the Node.js event loop to remain active. If there is no other activity keeping the event loop running, the process may exit before the Immediate object's callback is invoked. Calling immediate.unref() multiple times will have no effect.

[[Symbol.dispose]](): void

Cancels the immediate. This is similar to calling clearImmediate().

_onImmediate(...args: any[]): void

interface promises.Scheduler

Usage in Deno

import { type promises } from "node:timers";
type { Scheduler } = promises;

Methods

wait(
delay: number,
options?: { signal?: AbortSignal; },
): Promise<void>

An experimental API defined by the Scheduling APIs draft specification being developed as a standard Web Platform API.

Calling timersPromises.scheduler.wait(delay, options) is roughly equivalent to calling timersPromises.setTimeout(delay, undefined, options) except that the ref option is not supported.

import { scheduler } from 'node:timers/promises';

await scheduler.wait(1000); // Wait one second before continuing
yield(): Promise<void>

An experimental API defined by the Scheduling APIs draft specification being developed as a standard Web Platform API.

Calling timersPromises.scheduler.yield() is equivalent to calling timersPromises.setImmediate() with no arguments.


interface Timeout

extends RefCountedDisposableTimer,

Usage in Deno

import { type Timeout } from "node:timers";

This object is created internally and is returned from setTimeout() and setInterval(). It can be passed to either clearTimeout() or clearInterval() in order to cancel the scheduled actions.

By default, when a timer is scheduled using either setTimeout() or setInterval(), the Node.js event loop will continue running as long as the timer is active. Each of the Timeout objects returned by these functions export both timeout.ref() and timeout.unref() functions that can be used to control this default behavior.

Methods

close(): this

Cancels the timeout.

hasRef(): boolean

If true, the Timeout object will keep the Node.js event loop active.

ref(): this

When called, requests that the Node.js event loop not exit so long as the Timeout is active. Calling timeout.ref() multiple times will have no effect.

By default, all Timeout objects are "ref'ed", making it normally unnecessary to call timeout.ref() unless timeout.unref() had been called previously.

refresh(): this

Sets the timer's start time to the current time, and reschedules the timer to call its callback at the previously specified duration adjusted to the current time. This is useful for refreshing a timer without allocating a new JavaScript object.

Using this on a timer that has already called its callback will reactivate the timer.

unref(): this

When called, the active Timeout object will not require the Node.js event loop to remain active. If there is no other activity keeping the event loop running, the process may exit before the Timeout object's callback is invoked. Calling timeout.unref() multiple times will have no effect.

[[Symbol.toPrimitive]](): number

Coerce a Timeout to a primitive. The primitive can be used to clear the Timeout. The primitive can only be used in the same thread where the timeout was created. Therefore, to use it across worker_threads it must first be passed to the correct thread. This allows enhanced compatibility with browser setTimeout() and setInterval() implementations.

[[Symbol.dispose]](): void

Cancels the timeout.

_onTimeout(...args: any[]): void

interface TimerOptions

extends Abortable

Usage in Deno

import { type TimerOptions } from "node:timers";

Properties

ref: boolean | undefined
optional

Set to false to indicate that the scheduled Timeout should not require the Node.js event loop to remain active.


interface Timer

extends RefCounted

Usage in Deno

import { type Timer } from "node:timers";
Deprecated

Use NodeJS.Timeout instead.

Methods

hasRef(): boolean
refresh(): this
[[Symbol.toPrimitive]](): number

namespace promises

Usage in Deno

import { promises } from "node:timers";

The timers/promises API provides an alternative set of timer functions that return Promise objects. The API is accessible via require('node:timers/promises').

import {
  setTimeout,
  setImmediate,
  setInterval,
} from 'node:timers/promises';

Functions

promises.setImmediate
No documentation available
promises.setInterval

Returns an async iterator that generates values in an interval of delay ms. If ref is true, you need to call next() of async iterator explicitly or implicitly to keep the event loop alive.

promises.setTimeout
No documentation available

Interfaces

promises.Scheduler
No documentation available
  • wait
  • yield

Variables

promises.scheduler
No documentation available

See

  • source


variable promises.scheduler

Usage in Deno

import { promises } from "node:timers";
const { scheduler } = promises;

Type

Scheduler

© 2018–2025 the Deno authors
Licensed under the MIT License.
https://docs.deno.com/api/node/timers

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API