es-toolkit/docs/reference/function/debounce.md

63 lines
1.5 KiB
Markdown
Raw Normal View History

2024-04-25 14:56:13 +03:00
# debounce
Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds
have elapsed since the last time the debounced function was invoked. The debounced function also has a `cancel`
method to cancel any pending execution.
## Signature
```typescript
function debounce<F extends (...args: any[]) => void>(
func: F,
debounceMs: number,
options?: DebounceOptions
): F & { cancel: () => void };
2024-04-25 14:56:13 +03:00
```
### Parameters
2024-04-25 14:56:13 +03:00
- `func` (`F`): The function to debounce.
- `debounceMs` (`number`): The number of milliseconds to delay.
- `options` (`DebounceOptions`, optional): An options object.
- `signal` (`AbortSignal`, optional): An optional `AbortSignal` to cancel the debounced function.
2024-04-25 14:56:13 +03:00
### Returns
(`F & { cancel: () => void }`): A new debounced function with a `cancel` method.
2024-04-25 14:56:13 +03:00
## Examples
### Basic Usage
2024-04-25 14:56:13 +03:00
```typescript
const debouncedFunction = debounce(() => {
console.log('Function executed');
}, 1000);
// Will log 'Function executed' after 1 second if not called again in that time
debouncedFunction();
// Will not log anything as the previous call is canceled
debouncedFunction.cancel();
```
### Using with an AbortSignal
```typescript
const controller = new AbortController();
const signal = controller.signal;
const debouncedWithSignalFunction = debounce(
() => {
console.log('Function executed');
},
1000,
{ signal }
);
// Will log 'Function executed' after 1 second if not called again in that time
debouncedWithSignalFunction();
// Will cancel the debounced function call
controller.abort();
```