debounce

  • Version 3.0.0
  • Published
  • 8.59 kB
  • No dependencies
  • MIT license

Install

npm i debounce
yarn add debounce
pnpm add debounce

Overview

Delay function calls until a set time elapses after the last invocation

Index

Functions

function debounce

debounce: <F extends AnyFunction>(
function_: F,
wait?: number,
options?: Options
) => DebouncedFunction<F>;
  • Creates a debounced function that delays execution until wait milliseconds have passed since its last invocation.

    Parameter function_

    The function to debounce.

    Parameter wait

    The number of milliseconds to delay. Default: 100.

    Returns

    A debounced version of the function with additional control methods.

    Example 1

    import debounce from 'debounce';
    function resize() {
    console.log('height', window.innerHeight);
    console.log('width', window.innerWidth);
    }
    window.onresize = debounce(resize, 200);

Type Aliases

type AnyFunction

type AnyFunction = (...arguments_: readonly any[]) => unknown;

    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_: Parameters<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 an execution is scheduled, it will be immediately executed and the timer will be cleared.
    @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.
    @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.

    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 (1)

      Peer Dependencies (0)

      No peer dependencies.

      Badge

      To add a badge like this onejsDocs.io badgeto 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
        [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](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>