UNPKG

1.02 kBTypeScriptView Raw
1type AnyFunction = (...arguments_: readonly any[]) => unknown;
2
3/**
4Creates a debounced function that delays execution until `wait` milliseconds have passed since its last invocation.
5
6Set the `immediate` option to `true` to execute the function immediately at the start of the `wait` interval, preventing issues such as double-clicks on a button.
7
8The returned function has the following methods:
9
10- `.clear()` cancels any scheduled executions.
11- `.flush()` if an execution is scheduled then it will be immediately executed and the timer will be cleared.
12- `.trigger()` executes the function immediately and clears the timer if it was previously set.
13*/
14declare function debounce<F extends AnyFunction>(
15 function_: F,
16 wait?: number,
17 options?: {immediate: boolean}
18): debounce.DebouncedFunction<F>;
19
20declare namespace debounce {
21 type DebouncedFunction<F extends AnyFunction> = {
22 (...arguments_: Parameters<F>): ReturnType<F> | undefined;
23 clear(): void;
24 flush(): void;
25 trigger(): void;
26 };
27}
28
29export = debounce;