SHARE
TWEET

Untitled

a guest Sep 11th, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Returns a function, that, as long as it continues to be invoked, will not
  2. // be triggered. The function will be called after it stops being called for
  3. // N milliseconds. If `immediate` is passed, trigger the function on the
  4. // leading edge, instead of the trailing.
  5. function debounce(func, wait, immediate) {
  6.     var timeout;
  7.     return function() {
  8.         var context = this, args = arguments;
  9.         var later = function() {
  10.             timeout = null;
  11.             if (!immediate) func.apply(context, args);
  12.         };
  13.         var callNow = immediate && !timeout;
  14.         clearTimeout(timeout);
  15.         timeout = setTimeout(later, wait);
  16.         if (callNow) func.apply(context, args);
  17.     };
  18. };
  19.  
  20.  
  21. var myEfficientFn = debounce(function() {
  22.     // All the taxing stuff you do
  23. }, 250);
  24.  
  25. window.addEventListener('resize', myEfficientFn);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top