Before diving into code, let's visualize how these techniques control function execution flow.
Delays function execution until after a specified wait time has elapsed since the last event. Think of it as a "smart delay" that resets the timer with every new event.
Ensures a function executes at most once per specified time interval, regardless of how many events fire. Like a "traffic controller" that maintains a steady execution rate.
Move your mouse over the areas below to see the difference in real-time
Type in the search box below to see how debouncing prevents excessive API calls while typing.
Clean, production-ready code implementations
Leading & Trailing edge support
function debounce(func, wait, immediate = false) {
let timeout;
return function executedFunction(...args) {
const later = () => {
timeout = null;
if (!immediate) func.apply(this, args);
};
const callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(this, args);
};
}
// Usage
const handleSearch = debounce((query) => {
console.log('Searching for:', query);
// API call here
}, 300);
input.addEventListener('input', (e) => handleSearch(e.target.value));
With trailing execution option
function throttle(func, limit, trailing = true) {
let inThrottle;
let lastFunc;
let lastRan;
return function executedFunction(...args) {
const context = this;
if (!inThrottle) {
func.apply(context, args);
lastRan = Date.now();
inThrottle = true;
setTimeout(() => {
inThrottle = false;
if (trailing && lastFunc) {
lastFunc.apply(context, args);
lastFunc = null;
}
}, limit);
} else if (trailing) {
lastFunc = () => func.apply(context, args);
}
};
}
// Usage
const handleScroll = throttle(() => {
console.log('Scroll position:', window.scrollY);
}, 100);
window.addEventListener('scroll', handleScroll);
| Scenario | Technique | Reason |
|---|---|---|
| Search Input | Debounce | Wait for user to finish typing |
| Window Resize | Debounce | Recalculate layout after resize ends |
| Infinite Scroll | Throttle | Check scroll position at intervals |
| Button Clicks | Debounce | Prevent double submission |
| Mouse Move | Throttle | Update coordinates smoothly |