Lesson 22 — Debounce
Wait until the calls stop
Debounce delays a function until the calls stop for a moment. Each new call cancels the previous timer (clearTimeout) and starts a new one (setTimeout). So if a user types "c", "ca", "cat" quickly, only "cat" triggers the search.
const id = setTimeout(() => console.log("later"), 500);
clearTimeout(id); // cancelled: nothing is logged
...args passes on all the arguments the call received.
Why it matters: without debounce, a search box sends one request per keystroke.
Inside the returned function, cancel the old timer and start a new one, so only "cat" is searched.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
A quick 3-question quiz on this lesson — instant feedback.
Ready for more? Optional paid resources that pick up where this lesson leaves off.
Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.
Project-based JavaScript Foundations courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for JavaScript Foundations books — check the publication year so the examples match current versions.
Search Amazon →