Open History to see the last commit
1---
2id: TimeoutManager
3title: TimeoutManager
4redirect_from:
5 - reference/timeoutManager
6---
7β
8<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
9β
10Defined in: [packages/query-core/src/timeoutManager.ts:82](https://github.com/TanStack/query/blob/main/packages/query-core/src/timeoutManager.ts#L82)
11β
12Allows customization of how timeouts are created.
13β
14`@tanstack/query-core` makes liberal use of timeouts to implement `staleTime`
15and `gcTime`. The default TimeoutManager provider uses the platform's global
16`setTimeout` implementation, which is known to have scalability issues with
17thousands of timeouts on the event loop.
18β
19If you hit this limitation, consider providing a custom TimeoutProvider that
20coalesces timeouts.
21β
22## Implements
23β
24- `Omit`\<[`TimeoutProvider`](../type-aliases/TimeoutProvider.md), `"name"`\>
25β
26## Methods
27β
28### clearInterval()
29β
30```ts
31clearInterval(intervalId: ManagedTimerId | undefined): void;
32```
33β
34Defined in: [packages/query-core/src/timeoutManager.ts:240](https://github.com/TanStack/query/blob/main/packages/query-core/src/timeoutManager.ts#L240)
35β
36`clearInterval` can be used to cancel an interval, like the global
37`clearInterval` function. It should be called with an interval ID
38returned by `setInterval`.
39β
40#### Parameters
41β
42##### intervalId
43β
44[`ManagedTimerId`](../type-aliases/ManagedTimerId.md) \| `undefined`
45β
46The timer ID returned by `setInterval`, or `undefined`.
47β
48#### Returns
49β
50`void`
51β
52#### Example
53β
54```ts
55import { timeoutManager } from '@tanstack/query-core'
56β
57const intervalId = timeoutManager.setInterval(
58 () => console.log('ran at:', new Date()),
59 1000,
60)
61β
62timeoutManager.clearInterval(intervalId)
63```
64β
65#### Implementation of
66β
67```ts
68Omit.clearInterval
69```
70β
71***
72β
73### clearTimeout()
74β
75```ts
76clearTimeout(timeoutId: ManagedTimerId | undefined): void;
77```
78β
79Defined in: [packages/query-core/src/timeoutManager.ts:193](https://github.com/TanStack/query/blob/main/packages/query-core/src/timeoutManager.ts#L193)
80β
81`clearTimeout` cancels a timeout callback scheduled with `setTimeout`,
82like the global `clearTimeout` function. It should be called with a
83timer ID returned by `setTimeout`.
84β
85#### Parameters
86β
87##### timeoutId
88β
89[`ManagedTimerId`](../type-aliases/ManagedTimerId.md) \| `undefined`
90β
91The timer ID returned by `setTimeout`, or `undefined`.
92β
93#### Returns
94β
95`void`
96β
97#### Example
98β
99```ts
100import { timeoutManager } from '@tanstack/query-core'
101β
102const timeoutId = timeoutManager.setTimeout(
103 () => console.log('ran at:', new Date()),
104 1000,
105)
106β
107timeoutManager.clearTimeout(timeoutId)
108```
109β
110#### Implementation of
111β
112```ts
113Omit.clearTimeout
114```
115β
116***
117β
118### setInterval()
119β
120```ts
121setInterval(callback: TimeoutCallback, delay: number): ManagedTimerId;
122```
123β
124Defined in: [packages/query-core/src/timeoutManager.ts:216](https://github.com/TanStack/query/blob/main/packages/query-core/src/timeoutManager.ts#L216)
125β
126`setInterval` schedules a callback to be called approximately every
127`delay` milliseconds, like the global `setInterval` function.
128β
129Like `setTimeout`, it returns a timer ID, which may be a number or an
130object that can be coerced to a number via `Symbol.toPrimitive`.
131β
132#### Parameters
133β
134##### callback
135β
136[`TimeoutCallback`](../type-aliases/TimeoutCallback.md)
137β
138The function to call on every interval.
139β
140##### delay
141β
142`number`
143β
144The time between calls, in milliseconds.
145β
146#### Returns
147β
148[`ManagedTimerId`](../type-aliases/ManagedTimerId.md)
149β
150The timer ID, to pass to [TimeoutManager#clearInterval](#clearinterval).
151β
152#### Example
153β
154```ts
155import { timeoutManager } from '@tanstack/query-core'
156β
157const intervalId = timeoutManager.setInterval(
158 () => console.log('ran at:', new Date()),
159 1000,
160)
161```
162β
163#### Implementation of
164β
165```ts
166Omit.setInterval
167```
168β
169***
170β
171### setTimeout()
172β
173```ts
174setTimeout(callback: TimeoutCallback, delay: number): ManagedTimerId;
175```
176β
177Defined in: [packages/query-core/src/timeoutManager.ts:169](https://github.com/TanStack/query/blob/main/packages/query-core/src/timeoutManager.ts#L169)
178β
179`setTimeout` schedules a callback to run after approximately `delay`
180milliseconds, like the global `setTimeout` function. The callback can be
181canceled with `clearTimeout`.
182β
183It returns a timer ID, which may be a number or an object that can be
184coerced to a number via `Symbol.toPrimitive`.
185β
186#### Parameters
187β
188##### callback
189β
190[`TimeoutCallback`](../type-aliases/TimeoutCallback.md)
191β
192The function to call when the timeout elapses.
193β
194##### delay
195β
196`number`
197β
198The time to wait before calling `callback`, in milliseconds.
199β
200#### Returns
201β
202[`ManagedTimerId`](../type-aliases/ManagedTimerId.md)
203β
204The timer ID, to pass to [TimeoutManager#clearTimeout](#cleartimeout).
205β
206#### Example
207β
208```ts
209import { timeoutManager } from '@tanstack/query-core'
210β
211const timeoutId = timeoutManager.setTimeout(
212 () => console.log('ran at:', new Date()),
213 1000,
214)
215β
216const timeoutIdNumber: number = Number(timeoutId)
217```
218β
219#### Implementation of
220β
221```ts
222Omit.setTimeout
223```
224β
225***
226β
227### setTimeoutProvider()
228β
229```ts
230setTimeoutProvider<TTimerId extends ManagedTimerId>(provider: TimeoutProvider<TTimerId>): void;
231```
232β
233Defined in: [packages/query-core/src/timeoutManager.ts:118](https://github.com/TanStack/query/blob/main/packages/query-core/src/timeoutManager.ts#L118)
234β
235`setTimeoutProvider` can be used to set a custom implementation of the
236`setTimeout`, `clearTimeout`, `setInterval`, `clearInterval` functions,
237called a `TimeoutProvider`.
238β
239This may be useful if you notice event loop performance issues with
240thousands of queries. A custom TimeoutProvider could also support timer
241delays longer than the global `setTimeout` maximum delay value of about
24224 days.
243β
244It is important to call `setTimeoutProvider` before creating a
245QueryClient or queries, so that the same provider is used consistently
246for all timers in the application, since different TimeoutProviders
247cannot cancel each others' timers.
248β
249#### Type Parameters
250β
251##### TTimerId
252β
253`TTimerId` *extends* [`ManagedTimerId`](../type-aliases/ManagedTimerId.md)
254β
255#### Parameters
256β
257##### provider
258β
259[`TimeoutProvider`](../type-aliases/TimeoutProvider.md)\<`TTimerId`\>
260β
261The `TimeoutProvider` to use for all timers from now on.
262β
263#### Returns
264β
265`void`
266β
267#### Example
268β
269```ts
270import { timeoutManager, QueryClient } from '@tanstack/query-core'
271import { CustomTimeoutProvider } from './CustomTimeoutProvider'
272β
273timeoutManager.setTimeoutProvider(new CustomTimeoutProvider())
274β
275export const queryClient = new QueryClient()
276```