-
const throttle = (fn, wait) => {
let inThrottle, lastFn, lastTime;
return function() {
const context = this,
args = arguments;
if (!inThrottle) {
fn.apply(context, args);
lastTime = Date.now();
inThrottle = true;
} else {
clearTimeout(lastFn);
lastFn = setTimeout(function() {
// Can I ignore the following line of code?
// if (Date.now() - lastTime >= wait) {
fn.apply(context, args);
lastTime = Date.now();
// }
}, Math.max(wait - (Date.now() - lastTime), 0));
}
};
}; I don't understand why the same condition needs to be checked inside setTimeout. Can it be omitted? |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments
-
Yes, it can be omitted. I assume your path of thought went along the lines of the Setting a delay that imitates the time left for the throttle throws away the need for the conditional statement below: |
Beta Was this translation helpful? Give feedback.
-
Can it be omitted? Yes. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
`const throttle = (fn, wait) => { return function() { In this version, after the fn is executed, inThrottle is immediately set to true, and a timeout is started to set it back to false after the specified wait period. This ensures that there's a gap between subsequent function calls and the throttling mechanism is maintained. |
Beta Was this translation helpful? Give feedback.
-
Hi there! HiChen404 if (Date.now() - lastTime >= wait) {
fn.apply(context, args);
lastTime = Date.now();
} This condition checks whether enough time has passed since the last execution of the throttled function. If the time elapsed is greater than or equal to the specified The purpose of this condition is to handle cases where there's a delay between the The purpose of the condition outside the In some cases, it may be safe to omit the condition. For example, if the function is not time-sensitive and it does not matter if it is executed multiple times. However, in other cases, omitting the condition could lead to unexpected results. For example, if the function is used to throttle the execution of another function, omitting the condition could lead to the other function being executed more frequently than intended. |
Beta Was this translation helpful? Give feedback.
-
The condition inside the Here's why the condition inside
It can be definitely omitted but its suggested, you should not omit the condition inside |
Beta Was this translation helpful? Give feedback.
-
Thanks everyone for your patience. I can see there are already a couple of great answers here and the discussion is pretty useful for future readers, so I'll move this under discussions and mark the accepted answer. Cheers! 🍻 |
Beta Was this translation helpful? Give feedback.
Hi there! HiChen404
This condition checks whether enough time has passed since the last execution of the throttled function. If the time elapsed is greater than or equal to the specified
wait
time, the function is executed again.The purpose of this condition is to handle cases where there's a delay between the
setTimeout
being triggered and the actual execution of the function. If the delay is longer than the specifiedwait
time, the function should be executed immediately without waiting for the remaining time.The purpose of the condition outside the
setTimeout
block is to handle cases where ther…