Function debounce

  • Creates a debounced version of a function that delays invoking the provided function until after a specified wait time has elapsed since the last time it was invoked.

    Returns

    A debounced version of the original function that has the following behavior:

    • Delays execution until wait milliseconds have passed since the last call
    • If immediate is true, executes on the leading edge of the first call
    • If maxWait is provided, ensures the function is called at least once every maxWait milliseconds
    • Preserves the this context and arguments of the most recent call
    • Cancels pending executions when called again within the wait period

    Example

    // Basic debounce
    const debouncedSearch = debounce(searchAPI, 300);

    // With immediate execution
    const debouncedSave = debounce(saveData, 1000, true);

    // With maximum wait time
    const debouncedUpdate = debounce(updateUI, 200, false, 1000);

    Parameters

    • func: ((...args: any[]) => void)

      The function to debounce. Will receive any arguments passed to the debounced function.

        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    • wait: number

      The number of milliseconds to delay execution after the last call.

    • Optional immediate: boolean

      If true, the function will execute immediately on the first call, then start the debounce behavior for subsequent calls.

    • Optional maxWait: number

      The maximum time the function can be delayed before it's forcibly executed. If specified, the function will be called after this many milliseconds have passed since its last execution, regardless of the debounce wait time.

    Returns ((this: any, ...args: any[]) => void)

      • (this: any, ...args: any[]): void
      • Parameters

        • this: any
        • Rest ...args: any[]

        Returns void

Generated using TypeDoc