debounce
- Version 4.0.0
- Published
- 12.9 kB
- No dependencies
- MIT license
Install
npm i debounceyarn add debouncepnpm add debounceOverview
Delay function calls until a set time elapses after the last invocation
Index
Functions
Type Aliases
Functions
function debounce
debounce: <F extends AnyFunction>( function_: F, wait?: number, options?: Options | null) => DebouncedFunction<F>;Creates a debounced function that delays execution until
waitmilliseconds have passed since its last invocation.Parameter function_
The function to debounce.
Parameter wait
The number of milliseconds to delay, between
0and2147483647, the largest delay a timer can wait for. Default:100.Parameter options
Set
immediatetotrueto run on the leading edge instead of waiting.Returns
A debounced version of the function with additional control methods.
Throws
TypeError - If
function_is not a function, or ifoptionsis a boolean.Throws
RangeError - If
waitis negative,NaN, larger than the maximum timer delay, or a value that does not compare as a number such as a non-numeric string.Example 1
import debounce from 'debounce';function resize() {console.log('height', window.innerHeight);console.log('width', window.innerWidth);}const onResize = debounce(resize, 200);window.addEventListener('resize', onResize);
Type Aliases
type DebouncedFunction
type DebouncedFunction<F extends AnyFunction> = { /** Call the debounced function.
@returns The result of the original function, or `undefined` if the debounced function was not executed. */ (...arguments_: DebouncedParameters<F>): ReturnType<F> | undefined;
/** Indicates whether the debounce delay is currently active.
@example ``` import debounce from 'debounce';
const fn = debounce(() => console.log('Called'), 100);
fn(); console.log(fn.isPending); // true
fn.clear(); console.log(fn.isPending); // false ``` */ readonly isPending: boolean;
/** Cancels any scheduled executions.
@example ``` import debounce from 'debounce';
const fn = debounce(() => console.log('Called'), 100);
fn(); fn.clear(); // Cancels the pending execution // 'Called' is never logged ``` */ clear(): void;
/** If a call is waiting to run, it will be run immediately and the timer will be cleared. Does nothing when there is no call waiting to run, which includes an `immediate` call that already ran on the leading edge, and leaves any timer as it was. In `immediate` mode, the calls made during the wait do not run on their own, but `flush()` runs the last of them.
@example ``` import debounce from 'debounce';
const fn = debounce(() => console.log('Called'), 100);
fn(); fn.flush(); // Immediately executes // 'Called' is logged immediately ``` */ flush(): void;
/** Executes the function immediately and clears the timer if it was previously set. With no call waiting to run it still calls the function, with no arguments and no receiver.
@example ``` import debounce from 'debounce';
const fn = debounce(() => console.log('Called'), 100);
fn(); fn.trigger(); // Immediately executes // 'Called' is logged immediately ``` */ trigger(): void;};A debounced function with additional methods for controlling its behavior.
All callers share one timer. If the debounced function is on a prototype, calls from different instances within one wait collapse into one and the others are silently dropped, so give each instance its own debounced function instead.
Remarks
An overloaded function keeps only its last signature, and the type parameters of a generic function become
unknown.
type Options
type Options = { /** Execute the function immediately at the start of the wait interval, preventing issues such as double-clicks on a button.
@default false
@example ``` import debounce from 'debounce';
const saveInput = debounce(() => { console.log('Saving...'); }, 300, {immediate: true});
// First call executes immediately // Subsequent calls within 300ms are ignored saveInput(); saveInput(); // Ignored ``` */ readonly immediate?: boolean;};Package Files (1)
Dependencies (0)
No dependencies.
Dev Dependencies (3)
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/debounce.
- Markdown[](https://www.jsdocs.io/package/debounce)
- HTML<a href="https://www.jsdocs.io/package/debounce"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 3243 ms. - Missing or incorrect documentation? Open an issue for this package.
