Created
July 1, 2017 19:46
-
-
Save dmjcomdem/5143154eb9ea8d44ed6bbf4299e7d87a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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