Skip to content

Instantly share code, notes, and snippets.

@ielshareef
Created July 3, 2012 18:49
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 ielshareef/3041863 to your computer and use it in GitHub Desktop.
Save ielshareef/3041863 to your computer and use it in GitHub Desktop.
parallel execution and scope isolation in JavaScript
// functionality is broken across many code snippets wrapped in a function
var array_of_functions = [
function() {
// do something 1
},
function() {
// do something 2
},
function() {
// do something 3
},
function() {
// do something 4
}
];
// Start executing the snippets immediately with a 25 ms lag between each snippet.
setTimeout(function() {
// Get the first code snippet and remove it from the array
var item = array_of_functions.shift();
// execute the next code snippet after 25 ms from now
if (array_of_functions.length > 0) {
setTimeout(arguments.callee, 25);
}
// if the item isn't undefined, execute it
if(item){
item.call();
}
}, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment