Skip to content

Latest commit

 

History

History
23 lines (20 loc) · 483 Bytes

debounce-function.md

File metadata and controls

23 lines (20 loc) · 483 Bytes
title description author tags
Debounce Function
Delays a function execution until after a specified time.
technoph1le
debounce,performance
const debounce = (func, delay) => {
  let timeout;

  return (...args) => {
    clearTimeout(timeout);
    timeout = setTimeout(() => func(...args), delay);
  };
};

// Usage:
window.addEventListener(
  'resize',
  debounce(() => console.log('Resized!'), 500), // Will only output after resizing has stopped for 500ms
);