Guest User

Untitled

a guest
Nov 23rd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. /**
  2. *
  3. * @param {number} delay - milliseconds to delay the call
  4. * @param {function} fn - the callback to be fired when delay fires
  5. * @return {function} a function to call multiple times
  6. */
  7. module.exports = function debounce(delay, fn) {
  8. if (typeof delay !== 'number') {
  9. throw new Error('you must pass in a number');
  10. }
  11.  
  12. if (typeof fn !== 'function') {
  13. throw new Error('you must pass in a function');
  14. }
  15.  
  16. let timer;
  17.  
  18. return function () {
  19. clearTimeout(timer);
  20. timer = setTimeout.apply(setTimeout, [fn, delay, ...arguments]);
  21. };
  22. };
Add Comment
Please, Sign In to add comment