Write debounce and confirm no call has run yet after several calls.
Acceptance criteria
- 1.#out should show 0.
JavaScript Tutorial · JavaScript Performance
These 10 tasks go with the JavaScript Debounce chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “Debouncing delays a function until the calls have stopped for a while.” .
1.Build a debounce wrapperexercise · easy · 10 min
Write debounce and confirm no call has run yet after several calls.
Passes when: #out should show 0.
2.Show each call resets the timerexercise · easy · 10 min · practice plan
Confirm nothing has been received yet after three rapid calls.
Passes when: #out should show 0.
3.Simulate a search boxexercise · easy · 10 min · practice plan
Type several characters and confirm no search ran yet.
Passes when: #out should show 0.
4.Simulate rapid resize eventsexercise · easy · 10 min · practice plan
Fire many resize calls and confirm layout has not run yet.
Passes when: #out should show 0.
5.Pass arguments through the wrapperexercise · medium · 10 min · practice plan
Confirm the debounced function accepts arguments without error.
Passes when: #out should show function.
6.Fix the timer never clearedbug fix · easy · 8 min
Without clearTimeout, every call schedules its own run instead of collapsing into one.
Passes when: #out should show 1 ran.
7.Fix the function called immediatelybug fix · easy · 8 min · practice plan
The function should be scheduled, not run right away.
Passes when: #out should show 0.
8.Fix the timer variable shared across instancesbug fix · easy · 8 min · practice plan
Two separately created debounced functions should not share one timer — here the second call cancels the first.
Passes when: #out should show 2 ran.
9.Fix the arguments never forwardedbug fix · easy · 8 min · practice plan
The wrapped function should receive what it was called with.
Passes when: #out should show test.
10.Fix the wrapper missing its returnbug fix · easy · 8 min · practice plan
debounce must return the wrapped function for it to be usable.
Passes when: #out should show function.
Between them these tasks cover debounce, clearTimeout and Rapid calls.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.