Advertisement
vit134

Debounce

Dec 27th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Debounce
  3. */
  4.  
  5. function debounce(f, ms) {
  6.  
  7.   let timer = null;
  8.  
  9.   return function (...args) {
  10.     const onComplete = () => {
  11.       f.apply(this, args);
  12.       timer = null;
  13.     }
  14.  
  15.     if (timer) {
  16.       clearTimeout(timer);
  17.     }
  18.  
  19.     timer = setTimeout(onComplete, ms);
  20.   };
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement