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)


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