function throttle(fn, limit = 300) {
let lastRun = 0;
return function (...args) {
const now = Date.now();
if (now - lastRun >= limit) {
lastRun = now;
return fn.apply(this, args);
}
};
}
// Usage
const onScroll = throttle(() => {
console.log('scroll Y:', window.scrollY);
}, 200);
window.addEventListener('scroll', onScroll);
Create a free account and build your private vault. Share publicly whenever you want.