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