Created
July 3, 2012 18:49
-
-
Save ielshareef/3041863 to your computer and use it in GitHub Desktop.
parallel execution and scope isolation in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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