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