@sensimods/utility-lab
    Preparing search index...

    Function debounce

    • Creates a debounced version of a function that delays its execution until after delay milliseconds have elapsed since the last time it was called. Useful for performance optimization on things like search inputs or window resizing. The returned function includes a .cancel() method to clear any pending timers. This is essential for cleaning up in component-based frameworks (like React) to prevent updates on unmounted components.

      Type Parameters

      • T extends (...args: any[]) => any

      Parameters

      • func: T

        The function to debounce.

      • delay: number

        The number of milliseconds to wait.

      Returns (...args: Parameters<T>) => void & { cancel: () => void }

      A debounced function with a .cancel() method.

      const handleSearch = debounce((query: string) => console.log(query), 500);

      // Later, if the component unmounts:
      handleSearch.cancel();