leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2676.js (664B)


      1 /**
      2  * @param {Function} fn
      3  * @param {number} t
      4  * @return {Function}
      5  */
      6 
      7 var throttle = function(fn, t) {
      8   let timeoutInProgress = null;
      9   let argsToProcess = null;
     10   
     11   const timeoutFunction = () => {
     12     if (argsToProcess === null) {
     13       timeoutInProgress = null; // enter the waiting phase
     14     } else {
     15       fn(...argsToProcess);
     16       argsToProcess = null;
     17       timeoutInProgress = setTimeout(timeoutFunction, t);
     18     }
     19   };
     20 
     21   return function throttled(...args) {
     22     if (timeoutInProgress) {
     23       argsToProcess = args;
     24     } else {
     25       fn(...args); // enter the looping phase
     26       timeoutInProgress = setTimeout(timeoutFunction, t);
     27     }
     28   }
     29 };