# 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 #### clearInterval ```typescript function clearInterval(id: Timeout): void ``` (Safe copy) Cancels a timed, repeating action previously established by setInterval. **Parameters** | Name | Type | Description | | --- | --- | --- | | `id` | `Timeout` | The identifier of the interval to cancel. | **Example: Canceling an interval** ```typescript const intervalId = setInterval(() => console.log('tick'), 1000) // Stop after some condition clearInterval(intervalId) ``` #### clearTimeout ```typescript function clearTimeout(id: Timeout): void ``` (Safe copy) Cancels a timeout previously established by setTimeout. **Parameters** | Name | Type | Description | | --- | --- | --- | | `id` | `Timeout` | The identifier of the timeout to cancel. | **Example: Canceling a timeout** ```typescript const timerId = setTimeout(() => console.log('Never runs'), 5000) clearTimeout(timerId) ``` #### queueMicrotask ```typescript function queueMicrotask(callback: VoidFunction): void ``` (Safe copy) Queues a microtask to be executed before control returns to the event loop. **Parameters** | Name | Type | Description | | --- | --- | --- | | `callback` | `VoidFunction` | Function to execute. | **Example: Queuing a microtask** ```typescript console.log('Start') queueMicrotask(() => console.log('Microtask')) console.log('End') // Output: Start, End, Microtask ``` #### setInterval ```typescript function setInterval(callback: (args: TArgs) => void, delay?: number, ...args: TArgs): Timeout ``` (Safe copy) Repeatedly calls a function with a fixed time delay between each call. **Parameters** | Name | Type | Description | | --- | --- | --- | | `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** ```typescript let count = 0 const intervalId = setInterval(() => { count++ if (count >= 5) clearInterval(intervalId) }, 1000) ``` #### setTimeout ```typescript function setTimeout(callback: (args: TArgs) => void, delay?: number, ...args: TArgs): Timeout ``` (Safe copy) Sets a timer which executes a function once the timer expires. **Parameters** | Name | Type | Description | | --- | --- | --- | | `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** ```typescript const timerId = setTimeout(() => console.log('Executed'), 1000) // Pass arguments to callback setTimeout((name, count) => console.log(name, count), 500, 'items', 5) ``` ### Variables #### cancelAnimationFrame (Safe copy) Cancels an animation frame request previously scheduled. Available only in browser environments. ```typescript const cancelAnimationFrame: (handle: number) => void ``` **Value** `...` #### requestAnimationFrame (Safe copy) Requests the browser to call a function before the next repaint. Available only in browser environments. ```typescript const requestAnimationFrame: (callback: FrameRequestCallback) => number ``` **Value** `...` #### Timers (Safe copy) Namespace object containing all Timer/Scheduling functions. Note: Importing this imports all methods in this namespace (no tree-shaking). ```typescript const Timers: Readonly<{ cancelAnimationFrame: (handle: number) => void; clearInterval: (id: Timeout) => void; clearTimeout: (id: Timeout) => void; queueMicrotask: (callback: VoidFunction) => void; requestAnimationFrame: (callback: FrameRequestCallback) => number; setInterval: (callback: (args: TArgs) => void, delay?: number, args: TArgs) => Timeout; setTimeout: (callback: (args: TArgs) => void, delay?: number, args: TArgs) => Timeout }> ``` **Value** `...` --- Canonical page: https://www.hyperfrontend.dev/docs/libraries/utils/immutable-api/built-in-copy/timers/ This file: https://www.hyperfrontend.dev/docs/libraries/utils/immutable-api/built-in-copy/timers.md Documentation index: https://www.hyperfrontend.dev/llms.txt