TS-Scribe - v0.6.2
    Preparing search index...

    Function debounce

    • Creates a debounced version of a function, which will only be invoked after a specified delay has passed since the last time the debounced function was invoked. If immediate is true, the function will be triggered at the start of the debounce delay, otherwise, it is triggered after.

      This is useful for scenarios like limiting the rate of user input handling, resizing events, or scroll events.


      Example:

      const handleSearch = debounce(500, (searchTerm) => {
      console.log('Searching for:', searchTerm);
      });

      // In this example, `handleSearch` will only be called after 500ms since the last input
      // This prevents calling the function too frequently during rapid input changes.

      Type Parameters

      • T
      • R

      Parameters

      • wait: number

        The number of milliseconds to wait before invoking the function after the last call.

      • fn: GenericFunction<T, R>

        The function to debounce.

      • immediate: boolean = false

        If true, the function will be triggered at the beginning of the debounce period.

      Returns (this: T, arg: T) => void

      A debounced version of the input function.

      const debouncedLog = debounce(1000, (msg: string) => console.log(msg), true);
      debouncedLog("Hello"); // Immediately logs "Hello", then waits for 1000ms for further calls