JavaScript Quirks

Debounce Function

hardjs-quirksclosuretiming
0:00

Problem

Implement `debounce(fn, wait)` which returns a function that delays invoking `fn` until `wait` ms have elapsed since the last call. For the tests, call `debounce(fn, 10)` and verify it returns a callable function. We will manually test its behavior.

Your solution
Test results
Click Run tests to check your solution against the test cases.

About this challenge

Debounce Function is a hard javascript quirks coding challenge. It explores a surprising corner of JavaScript's behavior — the kind of gotcha that causes real bugs. Write your solution in the editor above, run it against the test cases, and get instant feedback — free, in your browser, no sign-up.

Show a hint & approach (spoiler)

Hint: Keep a `timer` variable in a closure. Each call clears the previous timer and schedules a new setTimeout.

Approach: Debouncing coalesces rapid bursts of calls into one invocation after quiet time. Classic use cases: search-as-you-type, window resize handlers.

Keep practicing

Browse all coding challenges, or build intuition for the underlying concepts with the algorithm visualizers and the developer cheatsheets.