Skip to content

Instantly share code, notes, and snippets.

@amio
Created June 29, 2016 10:45
Show Gist options
  • Save amio/134bd2300d1ef1dc721225c439e4a299 to your computer and use it in GitHub Desktop.
Save amio/134bd2300d1ef1dc721225c439e4a299 to your computer and use it in GitHub Desktop.
Throttled requestAnimationFrame
// Usage:
//
// var onScroll = traf(function () {
// item[0].style.width = window.scrollY + 100 + 'px';
// })
// window.addEventListener('scroll', onScroll, false);
export default function traf (fn) {
var ticking = false;
return function () {
if(!ticking) {
ticking = true;
requestAnimationFrame(function () {
ticking = false;
fn()
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment