Last active
August 29, 2015 14:13
-
-
Save pete-otaqui/906151124268ba35ecb3 to your computer and use it in GitHub Desktop.
Javascript Throttle
This file contains 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, 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