Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const debounce = (func, wait, immediate) => {
  2.   try {
  3.     let timeout;
  4.     return function() {
  5.       const context = this;
  6.       const args = arguments;
  7.       const later = function() {
  8.         timeout = null;
  9.         if (!immediate) func.apply(context, args);
  10.       };
  11.  
  12.       const callNow = immediate && !timeout;
  13.       clearTimeout(timeout);
  14.       timeout = setTimeout(later, wait);
  15.       if (callNow) func.apply(context, args);
  16.     };
  17.   } catch (e) {
  18.     console.log(e);
  19.   }
  20. };
  21.  
  22. const throttled = (delay, fn) => {
  23.   let lastCall = 0;
  24.   return (...args) => {
  25.     const now = new Date().getTime();
  26.     if (now - lastCall < delay) {
  27.       return;
  28.     }
  29.  
  30.     lastCall = now;
  31.     return fn(...args);
  32.   };
  33. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement