I was trying to create a web game but ran into trouble because of javascript's single-threaded-ness. I have a few functions like this
function printNum(currentNum, targetNum, source, delay) {
if(currentNum < targetNum) {
currentNum++;
setTimeout(function() {
for(var i=0; i<source.length; i++) {
source[i].innerHTML = ("<p>" + currentNum + "</p>");
}
printNum (currentNum, targetNum, source, delay);
},delay);
}
}
A few functions are called at once and they proceed asynchronously because of the setTimeOut() function. But I need my code after those functions to run when they fully finish (i.e. no printNum recursion anymore). I try to avoid putting all the code later into a callback function since the game works in a while loop. I wanna create something like this
while(condition is true){
/*some functions*/
/*asynchronous functions*/
/*wait till all asynchronous functions to finish*/
/*some functions*/
}
Should I create a special class to handle them (e.g. a queue)? Or is there already framework that does the same thing? Or maybe just modify my code?
Sorry if I am not clear about my question. I am new to this language, any help is sincerely appreciated.
whileloop, as that prevents the async procedures function running and will lock up the UI.