Your Submissions
You haven't submitted any code for this challenge yet. Solve the problem by passing all the test cases, and your submissions will appear here.
xxxxxxxxxx
36
function throttle(func, waitTime) {
// Set isThrottling flag to false to start
// and savedArgs to null
let isThrottling = false,
savedArgs = null;
// Spread the arguments for .apply
return function (args) {
// Return a wrapped function
// Flag preventing immediate execution
if (!isThrottling) {
// Actual initial function execution
func.apply(this, args);
// Flip flag to throttling state
isThrottling = true;
// Queue up timer to flip the flag so future iterations can occur
function queueTimer() {
setTimeout(() => {
// Stop throttling
isThrottling = false;
// Queueing up the next invocation after wait time passes
if (savedArgs) {
func.apply(this, savedArgs);
isThrottling = true;
savedArgs = null;
queueTimer();
}
}, waitTime);
OUTPUT
Results will appear here.