Debounce Function

Utility function to limit function execution rate.

Delays function execution until after a specified time has elapsed since the last call. Perfect for search inputs and resize handlers.
debounce.tstypescript
export function debounce<T extends (...args: any[]) => any>(
  func: T,
  wait: number
): (...args: Parameters<T>) => void {
  let timeout: NodeJS.Timeout | null = null;

  return function executedFunction(...args: Parameters<T>) {
    const later = () => {
      timeout = null;
      func(...args);
    };

    if (timeout) clearTimeout(timeout);
    timeout = setTimeout(later, wait);
  };
}

// Usage example
const handleSearch = debounce((query: string) => {
  console.log('Searching for:', query);
  // API call here
}, 300);

Usage

const debouncedFn = debounce(myFunction, 300);

Let’s Build Something You’ll Be Proud Of

No fluff. Just thoughtful design and reliable development.

Work with me
Average response time: within 24 hours