Loading problem…
This problem builds on throttle-function. Complete that first, then load your solution to continue.
Implement an enhanced throttle function that extends the basic throttle functionality with advanced features like cancellation, flushing, and return value handling.
fn and a delay delay in millisecondscancel() and flush()fn at most once per delay millisecondscancel() methodcancel() should cancel any pending executionflush() methodflush() should immediately execute any pending callfn when executedundefined if none)this context of the original function call// Example: Throttling with cancel
const handleScroll = (event) => {
console.log('Scroll position:', event.scrollY);
return event.scrollY;
};
const throttledScroll = throttle(handleScroll, 100);
window.addEventListener('scroll', throttledScroll);
throttledScroll.cancel(); // Cancels any pending execution
// Example: Flushing pending calls
const throttledFn = throttle((x) => x * 2, 100);
throttledFn(5); // Schedules execution
const result = throttledFn.flush(); // Executes immediately, returns 10