JavaScriptQuestion 05 of 36
What is event bubbling and throttling?
Answer out loud or in your head first, then compare.
Answer
Event bubbling
A click on a child element can propagate up to its parents.
HTML
<div id="parent">
<button id="child">Click</button>
</div>The event can travel:
button
↓
div
↓
body
↓
documentYou can stop the propagation:
JavaScript
event.stopPropagation();A practical use is event delegation.
Instead of adding a listener to 100 buttons:
JavaScript
list.addEventListener("click", event => {
if (event.target.matches("button")) {
// handle it
}
});Throttling
Limits how often a function runs.
For example, scroll can fire a handler many times per second. A throttle can limit it to at most one run every 200 ms.
Throttle: run regularly, but no more often than X.
Debounce: wait until the user stops doing the action.
Example:
scroll → often throttle
search input → often debounceHow did it go? Be honest — “Not yet” puts the question on your review list.
Related questions
- Task: write debounce — JavaScript
Practise in code
- Write debounce — runs tests in your browser