timers.d.ts (4839B)
1 /** 2 * The `timer` module exposes a global API for scheduling functions to 3 * be called at some future period of time. Because the timer functions are 4 * globals, there is no need to import `node:timers` to use the API. 5 * 6 * The timer functions within Node.js implement a similar API as the timers API 7 * provided by Web Browsers but use a different internal implementation that is 8 * built around the Node.js [Event Loop](https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#setimmediate-vs-settimeout). 9 * @see [source](https://github.com/nodejs/node/blob/v16.9.0/lib/timers.js) 10 */ 11 declare module "timers" { 12 import { Abortable } from "node:events"; 13 import { 14 setImmediate as setImmediatePromise, 15 setInterval as setIntervalPromise, 16 setTimeout as setTimeoutPromise, 17 } from "node:timers/promises"; 18 interface TimerOptions extends Abortable { 19 /** 20 * Set to `false` to indicate that the scheduled `Timeout` 21 * should not require the Node.js event loop to remain active. 22 * @default true 23 */ 24 ref?: boolean | undefined; 25 } 26 let setTimeout: typeof global.setTimeout; 27 let clearTimeout: typeof global.clearTimeout; 28 let setInterval: typeof global.setInterval; 29 let clearInterval: typeof global.clearInterval; 30 let setImmediate: typeof global.setImmediate; 31 let clearImmediate: typeof global.clearImmediate; 32 global { 33 namespace NodeJS { 34 // compatibility with older typings 35 interface Timer extends RefCounted { 36 hasRef(): boolean; 37 refresh(): this; 38 [Symbol.toPrimitive](): number; 39 } 40 interface Immediate extends RefCounted { 41 /** 42 * If true, the `Immediate` object will keep the Node.js event loop active. 43 * @since v11.0.0 44 */ 45 hasRef(): boolean; 46 _onImmediate: Function; // to distinguish it from the Timeout class 47 } 48 interface Timeout extends Timer { 49 /** 50 * If true, the `Timeout` object will keep the Node.js event loop active. 51 * @since v11.0.0 52 */ 53 hasRef(): boolean; 54 /** 55 * Sets the timer's start time to the current time, and reschedules the timer to 56 * call its callback at the previously specified duration adjusted to the current 57 * time. This is useful for refreshing a timer without allocating a new 58 * JavaScript object. 59 * 60 * Using this on a timer that has already called its callback will reactivate the 61 * timer. 62 * @since v10.2.0 63 * @return a reference to `timeout` 64 */ 65 refresh(): this; 66 [Symbol.toPrimitive](): number; 67 } 68 } 69 function setTimeout<TArgs extends any[]>( 70 callback: (...args: TArgs) => void, 71 ms?: number, 72 ...args: TArgs 73 ): NodeJS.Timeout; 74 // util.promisify no rest args compability 75 // eslint-disable-next-line @typescript-eslint/no-invalid-void-type 76 function setTimeout(callback: (args: void) => void, ms?: number): NodeJS.Timeout; 77 namespace setTimeout { 78 const __promisify__: typeof setTimeoutPromise; 79 } 80 function clearTimeout(timeoutId: NodeJS.Timeout | string | number | undefined): void; 81 function setInterval<TArgs extends any[]>( 82 callback: (...args: TArgs) => void, 83 ms?: number, 84 ...args: TArgs 85 ): NodeJS.Timer; 86 // util.promisify no rest args compability 87 // eslint-disable-next-line @typescript-eslint/no-invalid-void-type 88 function setInterval(callback: (args: void) => void, ms?: number): NodeJS.Timer; 89 namespace setInterval { 90 const __promisify__: typeof setIntervalPromise; 91 } 92 function clearInterval(intervalId: NodeJS.Timeout | string | number | undefined): void; 93 function setImmediate<TArgs extends any[]>( 94 callback: (...args: TArgs) => void, 95 ...args: TArgs 96 ): NodeJS.Immediate; 97 // util.promisify no rest args compability 98 // eslint-disable-next-line @typescript-eslint/no-invalid-void-type 99 function setImmediate(callback: (args: void) => void): NodeJS.Immediate; 100 namespace setImmediate { 101 const __promisify__: typeof setImmediatePromise; 102 } 103 function clearImmediate(immediateId: NodeJS.Immediate | undefined): void; 104 function queueMicrotask(callback: () => void): void; 105 } 106 } 107 declare module "node:timers" { 108 export * from "timers"; 109 }