Open History to see the last commit
1---
2id: debounce
3title: debounce
4---
5
6```ts
7function debounce<TFn>(fn, initialOptions): (...args) => void;
8```
9
10Defined in: [debouncer.ts:328](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/debouncer.ts#L328)
11
12Creates a debounced function that delays invoking the provided function until after a specified wait time.
13Multiple calls during the wait period will cancel previous pending invocations and reset the timer.
14
15This synchronous version is lighter weight and often all you need - upgrade to asyncDebounce when you need promises, retry support, abort/cancel capabilities, or advanced error handling.
16
17If leading option is true, the function will execute immediately on the first call, then wait the delay
18before allowing another execution.
19
20State Management:
21- Uses TanStack Store for reactive state management
22- Use `initialState` to provide initial state values when creating the debouncer
23- Use `onExecute` callback to react to function execution and implement custom logic
24- The state includes canLeadingExecute, execution count, and isPending status
25- State can be accessed via the underlying Debouncer instance's `store.state` property
26- When using framework adapters (React/Solid), state is accessed from the hook's state property
27
28## Type Parameters
29
30### TFn
31
32`TFn` *extends* [`AnyFunction`](../type-aliases/AnyFunction.md)
33
34## Parameters
35
36### fn
37
38`TFn`
39
40### initialOptions
41
42[`DebouncerOptions`](../interfaces/DebouncerOptions.md)\<`TFn`\>
43
44## Returns
45
46(...`args`) => `void`
47
48## Example
49
50```ts
51const debounced = debounce(() => {
52 saveChanges();
53}, { wait: 1000 });
54
55// Called repeatedly but executes at most once per second
56inputElement.addEventListener('input', debounced);
57```