@hyperfrontend/immutable-api-utils/built-in-copy/timers

timers

Locked, prototype-pollution-resistant copies of the global timer and scheduling functions.

setTimeout, setInterval, clearTimeout, clearInterval, queueMicrotask, and (where available) requestAnimationFrame / cancelAnimationFrame are captured at module-load time and frozen into a tamper-proof namespace, so scheduled work keeps firing on real host timers even if the globals are later patched. Effective only when imported before any untrusted code has had a chance to mutate the prototype chain.

API Reference

ƒ Functions

§function

clearInterval(id: Timeout): void

(Safe copy) Cancels a timed, repeating action previously established by setInterval.

Parameters

NameTypeDescription
§id
Timeout
The identifier of the interval to cancel.

Example

Canceling an interval

const intervalId = setInterval(() => console.log('tick'), 1000)
// Stop after some condition
clearInterval(intervalId)
§function

clearTimeout(id: Timeout): void

(Safe copy) Cancels a timeout previously established by setTimeout.

Parameters

NameTypeDescription
§id
Timeout
The identifier of the timeout to cancel.

Example

Canceling a timeout

const timerId = setTimeout(() => console.log('Never runs'), 5000)
clearTimeout(timerId)
§function

queueMicrotask(callback: VoidFunction): void

(Safe copy) Queues a microtask to be executed before control returns to the event loop.

Parameters

NameTypeDescription
§callback
VoidFunction
Function to execute.

Example

Queuing a microtask

console.log('Start')
queueMicrotask(() => console.log('Microtask'))
console.log('End')
// Output: Start, End, Microtask
§function

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

(Safe copy) Repeatedly calls a function with a fixed time delay between each call.

Parameters

NameTypeDescription
§callback
(args: TArgs) => void
Function to call at each interval.
§delay?
number
Time in milliseconds between calls.
§...args
TArgs
Additional arguments to pass to the callback.

Returns

Timeout
A numeric ID for the interval.

Example

Setting an interval

let count = 0
const intervalId = setInterval(() => {
  count++
  if (count >= 5) clearInterval(intervalId)
}, 1000)
§function

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

(Safe copy) Sets a timer which executes a function once the timer expires.

Parameters

NameTypeDescription
§callback
(args: TArgs) => void
Function to call when the timer elapses.
§delay?
number
Time in milliseconds before executing.
§...args
TArgs
Additional arguments to pass to the callback.

Returns

Timeout
A numeric ID for the timer.

Example

Setting a timeout

const timerId = setTimeout(() => console.log('Executed'), 1000)
// Pass arguments to callback
setTimeout((name, count) => console.log(name, count), 500, 'items', 5)

Variables

§type

cancelAnimationFrame

(Safe copy) Cancels an animation frame request previously scheduled. Available only in browser environments.
§type

requestAnimationFrame

(Safe copy) Requests the browser to call a function before the next repaint. Available only in browser environments.
§type

Timers

(Safe copy) Namespace object containing all Timer/Scheduling functions. Note: Importing this imports all methods in this namespace (no tree-shaking).