Debounce Polyfill

The event trigger is stopped for the delay from last event call. Where during each event call time gets reset.

Last Function Call + Delay => New Function Call

Normal Click: 0

Debounced Click: 0

function debounce(fn, delay) {
  let timerId;

  return function (...args) {
    if (timerId) {
      clearTimeout(timerId);
    }
    timerId = setTimeout(() => {
      fn.apply(this, args);
    }, delay);
  };
}