SHARE
TWEET

Untitled

a guest Sep 19th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function debounce(fn, debounceTime) {
  2.     if (typeof fn !== "function") {
  3.         throw "debounce arg is not a function";
  4.     }
  5.  
  6.     var time = debounceTime || 300;
  7.     var timeout;
  8.  
  9.     return function() {
  10.         if (timeout) {
  11.             clearTimeout(timeout);
  12.             timeout = undefined;
  13.         }
  14.  
  15.         timeout = setTimeout(fn, time);
  16.     };
  17. }
  18.  
  19. function throttle(fn, throttleTime) {
  20.     if (typeof fn !== "function") {
  21.         throw "throttle arg is not a function";
  22.     }
  23.  
  24.     var time = throttleTime || 300;
  25.     var scheduled = -1; // initial = -1, set - 1,  not set = 0
  26.  
  27.     return function() {
  28.         if (scheduled === 1) {
  29.             return;
  30.         }
  31.  
  32.         setTimeout(function() {
  33.             fn();
  34.             scheduled = 0;
  35.         }, time);
  36.  
  37.         scheduled = 1;
  38.     };
  39. }
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
 
Top