Skip to content

Instantly share code, notes, and snippets.

@ded
Created October 28, 2010 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ded/651974 to your computer and use it in GitHub Desktop.
Save ded/651974 to your computer and use it in GitHub Desktop.
A utility that throttles input to a timer
/**
* requires twttr.klass
*/
twttr.klass('twttr.Throttler', function(interval, callback) {
this.interval = interval;
this.callback = callback;
this._items = [];
})
.methods({
start: function() {
this._showNext();
this._timer = setInterval(this._showNext, this.interval);
},
stop: function() {
clearInterval(this.timer);
this._timer = null;
},
_showNext: function() {
var next = this.next();
if (next) {
this.callback(next);
}
},
add: function(item) {
this._items.unshift(item);
},
next: function() {
if (this._items.length) {
return this._items.shift();
}
return null;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment