Restrict how frequently a function may be invoked.
Note:
The original implemenation comes from underscore, except just to be a pain, I've flipped
the argument order so the function always comes last. This is to prevent
hard to read, dangling arguments when using anonymous functions or
long function invocations, such as when using bind
.
Plans: Dump confusing underscore implementation + implement fluent interface. Interface very likely to change.
$ component install timoxley/throttle
var throttle = require('throttle')
window.addEventListener('scroll', throttle(1000, function() {
console.log('I should only happen once every second.')
}))
window.addEventListener('scroll', throttle(2000, function() {
console.log('I should only happen once every two seconds.')
}))
Returns a new function that wraps the supplied fn
, blocking calls to fn
until wait
milliseconds have elapsed.
Returns a function, that, as long as it continues to be invoked, will not be triggered. The function will be called after it stops being called for wait
milliseconds.
MIT