jackmarketon
1/11/2018 - 10:10 PM

React Synthetic Supported Debouncer

Debouncer that support react synthetic events.

function debounce(func, wait, immediate) {
  var timeout;
  return (...args) => {
    const context = this;
    args[0].persist();
    const later = () => {
      timeout = null;
      if (!immediate) func.apply(context, args);
    };
    const callNow = immediate && !timeout;
    clearTimeout(timeout);
    timeout = setTimeout(later, wait);
    if (callNow) func.apply(context, args);
  };
};