throttle is debounce's twin and interviewers love asking them back to back. The one-line distinction to have ready: debounce waits for quiet; throttle fires on a steady cadence.
Remember when the function last ran. On each call, work out how much of the wait window remains:
{ leading, trailing } option.timer in the closure?" → so a single pending trailing call exists per throttled function.Write it from scratch, then add a { trailing: false } option that skips the trailing call. If you can explain when each edge matters, you're ready.
function throttle(fn, wait) { // your code here } // Try it: const log = throttle((x) => console.log('tick', x), 300); window.addEventListener?.('resize', () => log(Date.now()));
Test Code
Enter JavaScript that runs after your solution. It should return a value or a Promise.