basically, I have three functions, and what I want is to run all theses three functions in sequence (synchronous) and each function wait for the previous function finish. I've put the timeout within the functions to simulate a time execution, I don't know if this works. My code is.
//my three functions...
function wait1() {
setTimeout(function(){
console.log('hello, this is the function 1');
return 'ok';
},2000)
}
function wait2() {
setTimeout(function(){
console.log('hello, this is the function 2');
return 'ok';
},2000)
}
function wait3() {
setTimeout(function(){
console.log('hello, this is the function 3');
return 'ok';
},2000)
}
var tasks = [wait1,wait2,wait3];
var counter = 0;
function iterateTasks(tasks) {
runSequence(tasks[counter], function(){
counter++;
if(counter < tasks.length) {
iterateTasks(tasks);
}
});
}
//@params func received function
//@params cb received callback function
function runSequence(func,cb) {
var timeout = 0;
var tmr = setInterval(function(){
if(func() === 'ok' || timeout === 5000) {
console.log('OK, func = ', func);
cb();
clearInterval(tmr);
timeout = 0;
}
timeout += 500;
},500);
}
//start program...
iterateTasks(tasks);
Appreciate any help!