JavaScript Challenge: Day 16
Diffstat:
2 files changed, 30 insertions(+), 0 deletions(-)
@@ -0,0 +1,29 @@
/**
* @param {Function} fn
* @param {number} t
* @return {Function}
*/
var throttle = function(fn, t) {
let timeoutInProgress = null;
let argsToProcess = null;
const timeoutFunction = () => {
if (argsToProcess === null) {
timeoutInProgress = null; // enter the waiting phase
} else {
fn(...argsToProcess);
argsToProcess = null;
timeoutInProgress = setTimeout(timeoutFunction, t);
}
};
return function throttled(...args) {
if (timeoutInProgress) {
argsToProcess = args;
} else {
fn(...args); // enter the looping phase
timeoutInProgress = setTimeout(timeoutFunction, t);
}
}
};
@@ -555,3 +555,4 @@
for solving problems.
| 2665 | Easy | [Counter II](Problems/2665.js) |
| 2666 | Easy | [Allow One Function Call](Problems/2666.js) |
| 2667 | Easy | [Create Hello World Function](Problems/2667.js) |
| 2676 | Medium | [Throttle](Problems/2676.js) |