Implement throttle(fn, interval)
The sibling of debounce, and just as commonly asked, the two get confused constantly, which is exactly why interviewers ask for both.
Write a function throttle(fn, interval) that returns a new function. Calling the returned function should invoke fn immediately on the first call, then ignore any further calls until interval milliseconds have passed, at which point the next call is allowed through immediately again.
const onScroll = throttle(() => console.log("handling scroll"), 100);
// firing onScroll() 50 times in the same 100ms window only logs ONCEThe key difference from debounce: debounce waits for a pause in calls before firing; throttle fires immediately then rate-limits everything after.