Skip to content

Instantly share code, notes, and snippets.

@james-gardner
Created February 14, 2014 15:03
Show Gist options
  • Save james-gardner/9002561 to your computer and use it in GitHub Desktop.
Save james-gardner/9002561 to your computer and use it in GitHub Desktop.
Idea for a simple $.Deferred queue
var Queue = function () {
this.initialize.apply(this, arguments);
};
var p = Queue.prototype;
p.initialize = function () {
_.bindAll(this, ['check']);
this.items = [];
_.extend(this, Backbone.Events);
};
p.check = function () {
this.items = _.filter(this.items, function (r) {
return r.state() === 'pending';
});
if(this.items.length === 0) {
this.trigger('queue:complete');
}
},
p.add = function (func) {
this.items.push(func);
func.always(this.check);
};
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
var t = new Queue();
for(var i = 0; i < 25; i++ ) {
t.add($.Deferred(function (dfd) {
setTimeout(dfd.resolve, getRandomInt(1000, 8000));
}));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment