Skip to content

Instantly share code, notes, and snippets.

@pete-otaqui
Last active August 29, 2015 14:13
Show Gist options
  • Save pete-otaqui/906151124268ba35ecb3 to your computer and use it in GitHub Desktop.
Save pete-otaqui/906151124268ba35ecb3 to your computer and use it in GitHub Desktop.
Javascript Throttle
function throttle(fn, ms, scope) {
var last = 0;
var fn = function() {
var now = Date.now();
if ( now - last > ms ) {
last = now;
fn.apply(scope, arguments);
}
};
return scope ? fn.bind(scope) : fn;
}
/*
ele.addEventListener(
'scroll',
throttle(function(ev) {
// some slow operation where you don't
// want to use requestAnimationFrame
}, 1000/30, ele)
);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment