debounce.js 528 B

1234567891011121314151617
  1. const debounce_timeout_id = {};
  2. /**
  3. * Generic debounce helper
  4. *
  5. * @param {string} name - the name of the event to debounce
  6. * @param {function} func - the function to call after debouncing
  7. * @param {number} delay - the time in milliseconds to wait before calling the function
  8. */
  9. export default function debounce(name, func, delay) {
  10. const key = `${name}__${delay}`;
  11. clearTimeout(debounce_timeout_id[key]);
  12. debounce_timeout_id[key] = setTimeout(() => {
  13. func();
  14. delete debounce_timeout_id[key];
  15. }, delay);
  16. }