Timeoutclass

new Timeout(callback: Callback | undefined = undefined, ms = 0)
ParamType
callbackCallback
The default callback to run when a timeout fires (used by set() when no callback is passed). Defaults to undefined
msunknown
The default delay for any created timeouts (in ms). Defaults to 0
Return
Timeout
Stateful wrapper around setTimeout() that manages a single pending timeout at a time.
PropertyType
.existsboolean
Whether a timeout is currently pending. required readonly

Stateful wrapper around setTimeout() that manages a single pending timeout at a time.

  • Keeps track of the reference returned from setTimeout().
  • Clears any existing timeout when a new timeout is set.
  • Allows a default callback and a default delay to be set that are applied to all new timeouts that don't specify their own.

Examples

const timeout = new Timeout(() => console.log("fired"), 1000);
timeout.set(); // Fires the callback after 1000ms.
timeout.clear(); // Cancels it before it fires.

Methods

Go

Timeout.set()method

Cancel any existing timeout and set a new one.

set(callback: Callback | undefined = this._callback, ms: number = this._ms): void
Go

Timeout.clear()method

Cancel any existing timeout.

clear(): void