Skip to content

Instantly share code, notes, and snippets.

@dmjcomdem
Created July 1, 2017 19:46
Show Gist options
  • Save dmjcomdem/5143154eb9ea8d44ed6bbf4299e7d87a to your computer and use it in GitHub Desktop.
Save dmjcomdem/5143154eb9ea8d44ed6bbf4299e7d87a to your computer and use it in GitHub Desktop.
function throttle(fn, delay) {
let last;
let timer;
return () => {
const now = +new Date;
if (last && now < last + delay) {
clearTimeout(timer);
timer = setTimeout(() => {
last = now;
fn();
}, delay);
} else {
last = now;
fn();
}
};
}
window.addEventListener('scroll', throttle(onScroll, 25));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment