debounce.js 995 B

1234567891011121314151617181920212223242526272829303132
  1. 'use strict';
  2. /**
  3. * @link https://davidwalsh.name/javascript-debounce-function
  4. * @param func needs to implement a function which is debounced
  5. * @param wait how long do you want to wait till the previous declared function is executed
  6. * @param isImmediate defines if you want to execute the function on the first execution or the last execution inside the time window. `true` for first and `false` for last.
  7. */
  8. var debounce = ((func, wait, isImmediate) => {
  9. let timeout;
  10. return function debounce(...args) {
  11. const context = this;
  12. const later = () => {
  13. timeout = undefined;
  14. if (!isImmediate) {
  15. func.apply(context, args);
  16. }
  17. };
  18. const shouldCallNow = isImmediate && !timeout;
  19. if (timeout !== undefined) {
  20. clearTimeout(timeout);
  21. }
  22. timeout = setTimeout(later, wait);
  23. if (shouldCallNow) {
  24. return func.apply(context, args);
  25. }
  26. return undefined;
  27. };
  28. });
  29. module.exports = debounce;
  30. //# sourceMappingURL=debounce.js.map