Turning Learners Into Developers
Codekilla
CODEKILLA
Medium
JS-002javascript

Implement Debounce

Problem

Write `debounce(fn, ms)` that returns a function which, when called repeatedly, will fire `fn` only once `ms` milliseconds have passed since the last call. This is the pattern behind 'search-as-you-type' that doesn't hammer the server.

Input
N/A (functional programming problem — print arbitrary proof via console.log).
Output
Single line: `fired=<count>` after rapid calls.
Constraints
fn is called 10 times with 50ms gaps; debounce ms=200. Expected: fires exactly once.
Sample input
(calls 10 times at 50ms intervals; final wait 300ms)
Sample output
fired=1
Explanation
Each call resets the timer; fn only runs when 200ms of silence passes.
closurestimersfunctionaldesign@Facebook@Airbnb@Shopify
Visible test cases
in: N/A
out: fired=1
Your solution — run it, use AI if stuck
javascript