Skip to content

Instantly share code, notes, and snippets.

@austinbv
Created July 11, 2012 04:53
Show Gist options
  • Save austinbv/3088101 to your computer and use it in GitHub Desktop.
Save austinbv/3088101 to your computer and use it in GitHub Desktop.
(function() {
var sleep = function(ms, callback) {
var start = new Date().getTime(), finished;
while(!finished) {
if ((new Date().getTime() - start) > ms){
finished = true;
}
}
if (callback)
callback();
};
var randomTimeout = function() {
return Math.ceil(Math.random() * 3000);
};
var noCallback = function(number) {
sleep(randomTimeout());
console.log("function: " + number);
};
var withCallback = function(number, callback) {
callback(number);
};
var consoleLog = function(number) {
sleep(randomTimeout());
console.log(number);
};
noCallback(1);
noCallback(2);
noCallback(3);
noCallback(4);
noCallback(5);
noCallback(6);
noCallback(7);
noCallback(8);
noCallback(9);
noCallback(10);
console.log('--------- With Callback ---------');
withCallback(1, consoleLog);
withCallback(2, consoleLog);
withCallback(3, consoleLog);
withCallback(4, consoleLog);
withCallback(5, consoleLog);
withCallback(6, consoleLog);
withCallback(7, consoleLog);
withCallback(8, consoleLog);
withCallback(9, consoleLog);
withCallback(10, consoleLog);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment