I have a workflow on JS, which should run several set by setTimeout functions one by one. How this could be done with JS/jQuery, preferably in some easy and beautiful way?
It looks like this
function recursiveOne(arg1){
if (allRight) return;
doSomething();
andAnotherOne();
setTimeout(function(){recursiveOne(arg1)}, 3000);
}
function coreFunction(){
recursiveOne(arg1);
recursiveTwo(arg2);
recursiveThree(arg3);
}
where recursiveTwo should start only when recursiveOne already done its last iteration.
The bad part is all functions work through setTimeout because I need to wait reaction from backend, and couldn’t receive it directly – only by HTML source.
Possible solutions, that I could see:
- next function callback passed right in the previous function. Not too cool.
- jQuery deffered object, which isn’t so beatiful too, but a bit better. The downside is I still should rise additional deffered request in each function I want to use this way.
You have to use a callback or invoke
coreFunctiondirectly. Below you can find a way to do it using array of functions.