Home Reference Source

ts_temp/function/debounce.js

  1. import curryN from './curryN';
  2. /**
  3. * Creates a debounced function that delays invoking `func` until after `wait`
  4. * milliseconds have elapsed since the last time the debounced function was
  5. * invoked. Delayed function invocation might be cancelled by calling cancel method.
  6. *
  7. * @param {number} wait The number of milliseconds to delay.
  8. * @param {Function} fn The function to debounce.
  9. * @returns {Function} Returns the new debounced function.
  10. */
  11. export default curryN(2, (wait, fn) => {
  12. let timeout;
  13. function f() {
  14. let args = arguments;
  15. clearTimeout(timeout);
  16. timeout = setTimeout(() => fn.apply(this, args), // eslint-disable-line prefer-rest-params
  17. wait);
  18. }
  19. f.cancel = () => clearTimeout(timeout);
  20. return f;
  21. });