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:70](https://github.com/TanStack/query/blob/main/packages/query-core/src/timeoutManager.ts#L70)
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:224](https://github.com/TanStack/query/blob/main/packages/query-core/src/timeoutManager.ts#L224)
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β
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[`TimeoutProvider`](../type-aliases/TimeoutProvider.md).[`clearInterval`](../type-aliases/TimeoutProvider.md#clearinterval)
64β
65***
66β
67### clearTimeout()
68β
69```ts
70clearTimeout(timeoutId: ManagedTimerId | undefined): void;
71```
72β
73Defined in: [packages/query-core/src/timeoutManager.ts:179](https://github.com/TanStack/query/blob/main/packages/query-core/src/timeoutManager.ts#L179)
74β
75`clearTimeout` cancels a timeout callback scheduled with `setTimeout`,
76like the global `clearTimeout` function. It should be called with a
77timer ID returned by `setTimeout`.
78β
79#### Parameters
80β
81##### timeoutId
82β
83[`ManagedTimerId`](../type-aliases/ManagedTimerId.md) | `undefined`
84β
85#### Returns
86β
87`void`
88β
89#### Example
90β
91```ts
92import { timeoutManager } from '@tanstack/query-core'
93β
94const timeoutId = timeoutManager.setTimeout(
95 () => console.log('ran at:', new Date()),
96 1000,
97)
98β
99timeoutManager.clearTimeout(timeoutId)
100```
101β
102#### Implementation of
103β
104[`TimeoutProvider`](../type-aliases/TimeoutProvider.md).[`clearTimeout`](../type-aliases/TimeoutProvider.md#cleartimeout)
105β
106***
107β
108### setInterval()
109β
110```ts
111setInterval(callback: TimeoutCallback, delay: number): ManagedTimerId;
112```
113β
114Defined in: [packages/query-core/src/timeoutManager.ts:200](https://github.com/TanStack/query/blob/main/packages/query-core/src/timeoutManager.ts#L200)
115β
116`setInterval` schedules a callback to be called approximately every
117`delay` milliseconds, like the global `setInterval` function.
118β
119Like `setTimeout`, it returns a timer ID, which may be a number or an
120object that can be coerced to a number via `Symbol.toPrimitive`.
121β
122#### Parameters
123β
124##### callback
125β
126[`TimeoutCallback`](../type-aliases/TimeoutCallback.md)
127β
128##### delay
129β
130`number`
131β
132#### Returns
133β
134[`ManagedTimerId`](../type-aliases/ManagedTimerId.md)
135β
136#### Example
137β
138```ts
139import { timeoutManager } from '@tanstack/query-core'
140β
141const intervalId = timeoutManager.setInterval(
142 () => console.log('ran at:', new Date()),
143 1000,
144)
145```
146β
147#### Implementation of
148β
149[`TimeoutProvider`](../type-aliases/TimeoutProvider.md).[`setInterval`](../type-aliases/TimeoutProvider.md#setinterval)
150β
151***
152β
153### setTimeout()
154β
155```ts
156setTimeout(callback: TimeoutCallback, delay: number): ManagedTimerId;
157```
158β
159Defined in: [packages/query-core/src/timeoutManager.ts:155](https://github.com/TanStack/query/blob/main/packages/query-core/src/timeoutManager.ts#L155)
160β
161`setTimeout` schedules a callback to run after approximately `delay`
162milliseconds, like the global `setTimeout` function. The callback can be
163canceled with `clearTimeout`.
164β
165It returns a timer ID, which may be a number or an object that can be
166coerced to a number via `Symbol.toPrimitive`.
167β
168#### Parameters
169β
170##### callback
171β
172[`TimeoutCallback`](../type-aliases/TimeoutCallback.md)
173β
174##### delay
175β
176`number`
177β
178#### Returns
179β
180[`ManagedTimerId`](../type-aliases/ManagedTimerId.md)
181β
182#### Example
183β
184```ts
185import { timeoutManager } from '@tanstack/query-core'
186β
187const timeoutId = timeoutManager.setTimeout(
188 () => console.log('ran at:', new Date()),
189 1000,
190)
191β
192const timeoutIdNumber: number = Number(timeoutId)
193```
194β
195#### Implementation of
196β
197[`TimeoutProvider`](../type-aliases/TimeoutProvider.md).[`setTimeout`](../type-aliases/TimeoutProvider.md#settimeout)
198β
199***
200β
201### setTimeoutProvider()
202β
203```ts
204setTimeoutProvider<TTimerId>(provider: TimeoutProvider<TTimerId>): void;
205```
206β
207Defined in: [packages/query-core/src/timeoutManager.ts:106](https://github.com/TanStack/query/blob/main/packages/query-core/src/timeoutManager.ts#L106)
208β
209`setTimeoutProvider` can be used to set a custom implementation of the
210`setTimeout`, `clearTimeout`, `setInterval`, `clearInterval` functions,
211called a `TimeoutProvider`.
212β
213This may be useful if you notice event loop performance issues with
214thousands of queries. A custom TimeoutProvider could also support timer
215delays longer than the global `setTimeout` maximum delay value of about
21624 days.
217β
218It is important to call `setTimeoutProvider` before creating a
219QueryClient or queries, so that the same provider is used consistently
220for all timers in the application, since different TimeoutProviders
221cannot cancel each others' timers.
222β
223#### Type Parameters
224β
225##### TTimerId
226β
227`TTimerId` *extends* [`ManagedTimerId`](../type-aliases/ManagedTimerId.md)
228β
229#### Parameters
230β
231##### provider
232β
233[`TimeoutProvider`](../type-aliases/TimeoutProvider.md)\<`TTimerId`\>
234β
235#### Returns
236β
237`void`
238β
239#### Example
240β
241```ts
242import { timeoutManager, QueryClient } from '@tanstack/query-core'
243import { CustomTimeoutProvider } from './CustomTimeoutProvider'
244β
245timeoutManager.setTimeoutProvider(new CustomTimeoutProvider())
246β
247export const queryClient = new QueryClient()
248```