Debounce Function in JavaScript

Created on 5/29/2025, 3:25:46 PM

function debounce(func, delay) {
  let timeoutId;
  return function(...args) {
    clearTimeout(timeoutId);
    timeoutId = setTimeout(() => {
      func.apply(this, args);
    }, delay);
  };
}

// Usage
window.addEventListener('resize', debounce(() => {
  console.log('Window resized');
}, 300));