Utilities for timers.
npm install --save extra-timers
# or
yarn add extra-timersfunction setTimeout(
timeout: number
, callback: () => unknown
): () => voidA wrapper for globalThis.setTimeout, with the following differences:
- Better order of parameters.
- The return value is the function to cancel the timer.
- It works correctly on Node.js #26578.
- It works correctly when
timeoutisInfinity. - It works correctly when
timeoutis greater than2147483647.
function setSchedule(
timestamp: number
, callback: () => unknown
): () => voidA wrapper for setTimeout.
function setInterval(
timeout: number
, callback: () => unknown
): () => voidA wrapper for setTimeout instead of globalThis.setInterval.
function setImmediate(
callback: () => unknown
): () => voidA wrapper for gloalThis.setImmediate and setTimeout(0, callback).
function setTimeoutLoop(
timeout: number
, callback: () => Awaitalbe<unknown>
): () => voidCreate an interval timer using the nested setTimeout,
which does not schedule the next run until the callback function returns.
The return value is the function to cancel the timer.
function setDynamicTimeoutLoop(
timeout: number
, callback: () => Awaitable<unknown>
): () => voidCreate an interval timer using the nested setTimeout,
which does not schedule the next run until the callback function returns,
and dynamically adjusts the interval time based on the execution time of the callback function.
The return value is the function to cancel the timer.
function calculateExponentialBackoffTimeout({
baseTimeout
, retries
, maxTimeout = Infinity
, factor = 2
, jitter = true
}: {
baseTimeout: number
retries: number
maxTimeout?: number
factor?: number
jitter?: boolean
}): number