Skip to content

Instantly share code, notes, and snippets.

@zhuochun
Created April 21, 2013 10:11
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 zhuochun/5429151 to your computer and use it in GitHub Desktop.
Save zhuochun/5429151 to your computer and use it in GitHub Desktop.
Run a large batch of async tasks separated
var complete = {}, completed = 0;
var i, thread = 23, total = 179, max = (total / thread | 0) + 1;
function startCount(idx, max) {
if (completed >= total) {
console.log("completed = " + completed);
console.log(complete);
return ;
} else if (idx === max || idx >= total) {
console.log("idx = " + idx + ", max = " + max);
console.log(complete);
return ;
}
setTimeout(function() {
complete[idx] = true;
completed++;
console.log(idx + " done, completed " + completed);
startCount(idx + 1, max);
}, (Math.random() * 100) | 0);
}
console.log("thread = " + thread + ", max = " + max);
for (i = 0; i < thread; i++) {
console.log("start " + (max * i) + " to " + (max * (i + 1)));
startCount(max * i, max * (i + 1));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment