← All posts
postApril 10, 2026
Throttle — Rate-Limit Function Calls
#javascript#performance#throttle

javascript
function throttle(fn, limit) {
let inThrottle = false;
return function(...args) {
if (!inThrottle) {
fn.apply(this, args);
inThrottle = true;
setTimeout(() => (inThrottle = false), limit);
}
};
}
const onScroll = throttle(() => {
updateParallax();
}, 16); // ~60fps
window.addEventListener('scroll', onScroll);