On this page

C

Timeout

History

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.

M

timeout.close

History
timeout.close(): Timeout
Stability: 3Legacy: Use clearTimeout() instead.
Returns:Timeout
a reference to timeout

Cancels the timeout.

M

timeout.hasRef

History
timeout.hasRef(): boolean
Returns:boolean

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

M

timeout.ref

History
timeout.ref(): Timeout
Returns:Timeout
a reference to timeout

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.

M

timeout.refresh

History
timeout.refresh(): Timeout
Returns:Timeout
a reference to timeout

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.

M

timeout.unref

History
timeout.unref(): Timeout
Returns:Timeout
a reference to timeout

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.

M

timeout[Symbol.toPrimitive]

History
timeout[Symbol.toPrimitive](): integer
Returns:integer
a number that can be used to reference this timeout

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.

M

timeout[Symbol.dispose]

History
timeout[Symbol.dispose](): void

Cancels the timeout.