extra-timers
v0.3.0
Published
Utilities for timers
Downloads
3,745
Maintainers
Readme
extra-timers
Utilities for timers.
Install
npm install --save extra-timers
# or
yarn add extra-timersAPI
setTimeout
function setTimeout(timeout: number, cb: () => 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 (nodejs#26578)
- It works correctly when
timeoutisInfinity. - It works correctly when
timeoutis greater than2147483647.
setSchedule
function setSchedule(timestamp: number, cb: () => unknown): () => voidA wrapper for setTimeout.
setInterval
function setInterval(timeout: number, cb: () => unknown): () => voidA wrapper for setTimeout instead of globalThis.setInterval.
setImmediate
function setImmediate(cb: () => unknown): () => voidA wrapper for gloalThis.setImmediate and setTimeout(0, cb).
setTimeoutLoop
function setTimeoutLoop(timeout: number, cb: () => 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.
setDynamicTimeoutLoop
function setDynamicTimeoutLoop(timeout: number, cb: () => 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.
calculateExponentialBackoffTimeout
function calculateExponentialBackoffTimeout({
baseTimeout
, retries
, maxTimeout = Infinity
, factor = 2
, jitter = true
}: {
baseTimeout: number
retries: number
maxTimeout?: number
factor?: number
jitter?: boolean
}): number