Created
December 19, 2014 01:33
-
-
Save draggor/74684740132f836c601a to your computer and use it in GitHub Desktop.
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
function latch(n, done, notDone) { | |
return function() { | |
if(--n === 0) { | |
done(); | |
} else if(n > 0) { | |
notDone(n); | |
} | |
} | |
} | |
function getRandomNumbers() { | |
var n = Math.floor(Math.random() * 100); | |
var a = []; | |
for(var i = 0; i < n; i++) { | |
a.push(Math.floor(Math.random() * 100)); | |
} | |
return a; | |
} | |
function step1(data, done) { | |
if(done) { | |
step2(data); | |
} else { | |
step1a(data); | |
} | |
} | |
var randomNumbers = getRandomNumbers(); | |
function step1a(data) { | |
var cb = function() { | |
if(randomNumbers.length === 0) { | |
step1(data, true); | |
} else { | |
data.arr = data.arr.concat(randomNumbers.splice(0, 10)); | |
step1(data); | |
} | |
}; | |
setTimeout(cb, 1000); | |
} | |
function step2(data) { | |
var l = latch(data.arr.length, function() { console.log('done'); console.log(data.arr); }, function(n) { console.log(n + ' to go!'); }); | |
for(var i = 0; i < data.arr.length; i++) { | |
var t = Math.floor(Math.random() * 10000) + 1000; | |
var cb = (function(j) { | |
return function() { | |
console.log('Index: ' + j + ', Data: ' + data.arr[j]); | |
l(); | |
}; | |
})(i); | |
setTimeout(cb, t); | |
} | |
} | |
step1({arr:[]}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment