Skip to content

Instantly share code, notes, and snippets.

@rutger1140
Created June 11, 2014 14:25
Show Gist options
  • Save rutger1140/691c865154736508332e to your computer and use it in GitHub Desktop.
Save rutger1140/691c865154736508332e to your computer and use it in GitHub Desktop.
Debouncing scroll events
// Sample code from Paul Lewis
// Source: http://www.html5rocks.com/en/tutorials/speed/animations/
var latestKnownScrollY = 0,
ticking = false;
function onScroll() {
latestKnownScrollY = window.scrollY;
requestTick();
}
function requestTick() {
if(!ticking) {
requestAnimationFrame(update);
}
ticking = true;
}
function update() {
ticking = false;
var currentScrollY = latestKnownScrollY;
console.log("Update!");
}
window.addEventListener('scroll', onScroll, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment