Class Debouncer<F>

A class for debouncing a function. That means multiple calls to the same function will only fire a period of time after the first, so that the same function can be requested quickly but only fired after the time completed

Example

const handleResize = () => {
console.log("Window resized!");
};

const debouncer = new Debouncer(handleResize, 300);
globalThis.addEventListener("resize", () => debouncer.run());
globalThis.addEventListener("click", () => debouncer.forceRun());
globalThis.addEventListener("dblclick", () => debouncer.cancel());

Type Parameters

  • F extends ((...args) => void)

Constructors

Methods

Constructors

  • Type Parameters

    • F extends ((...args) => void)

    Parameters

    • func: F

      The function. The parameters are determined by what is passed to the run() function.

    • delay: number

      Time in ms

    • immediate: boolean = false

      when true, run immediately on the first call to run.

    Returns Debouncer<F>

Methods

  • Attempt to call the function. Pass in the functions parameters. It will not run until the delay is finished, and will be replaced by the next call to run if the delay isn't finished.

    Parameters

    • Rest ...args: Parameters<F>

    Returns void

  • Run the function immediately, stopping any previous timers. After this, another call to run will work as if its the first time called.

    Parameters

    • Rest ...args: Parameters<F>

    Returns void

Generated using TypeDoc v0.25.12