Skip to content

Instantly share code, notes, and snippets.

@supersheep
Created August 25, 2012 15:19
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 supersheep/3467003 to your computer and use it in GitHub Desktop.
Save supersheep/3467003 to your computer and use it in GitHub Desktop.
A simple javascript async queue
var list = [{
time:1000,
proc:function(){
console.log("after 1 second");
}
},{
time:5000,
proc:function(){
console.log("after 5 seconds");
}
},{
time:2000,
proc:function(){
console.log("after 2 seconds")
}
}];
(function(list){
var queue;
function deal(item,i){
function func(){
setTimeout(function(){
item.proc();
queue[i+1] && queue[i+1]();
},item.time);
}
if(queue){
queue.push(func);
}else{
queue = [];
func();
}
}
list.forEach(deal);
})(list);
// check http://foliotek.github.com/AjaxQ/ajaxq.js later
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment