2

I have a for loop that runs through an array of functions and invokes each one. ideally I would like to invoke them in a particular sequence since the output of one might effect the output of the others after it. is there anyway to do that without setting time intervals or timers?

Sorry...I guess i should have explained a bit more.. the example i have in mind might sometimes use ajax which means I would have to wait for ajax success before i carry on. ( other times I dont need to wait for ajax success) so i would like to set a condition such as if (success) { continue the for loop } in which success is a variable set on the success of each part of the loop.

2 Answers 2

6

Javascript runs synchronously, meaning that no two functions can be executing at the same time. So long as you run them in the correct order, you shouldn't have any problems.

When waiting for an ajax response you could either set the XMLHttpRequest to run synchronously (so, sjax rather than ajax) or set up some sort of function iterator that works like the following:

var FunctionIterator = {
   nextFunction: 0,
   functionArray: [],
   callNext: function ()
   {
       this.functionArray[this.nextFunction].apply(null, arguments);
       this.nextFunction++;
   }
}

Then you use it like:

FunctionIterator.functionArray.push(someFunctionName);
FunctionIterator.functionArray.push(function (arg1) { alert(arg1); });

and

FunctionIterator.callNext(arg1, arg2, arg3);

Maybe self-explanatory but each time you run the callNext function, the next function is called and the array index is increased by 1 in preparation for the next function call.

Sign up to request clarification or add additional context in comments.

5 Comments

Sorry...I guess i should have explained a bit more.. the example i have in mind might sometimes use ajax which means I would have to wait for ajax success before i carry on. ( other times I dont need to wait for ajax success) so i would like to set a condition such as if (success) { continue the for loop } in which success is a variable set on the success of each part of the loop.
Salmane, edit your question to reflect the above comment, also some example code may be useful.
@salmane: could you just use synchronous xhrs for the ones that need to delay script execution? See my updated answer.
Note that in your first example, you don't need to convert the arguments object to array, you can just pass it along to apply, it can use array-like objects...
@CMS: I thought so but I wasn't sure and didn't have the time to test it. Thanks for clarifying that for me.
0

If the functions run async you could also set them up to be called recursively (code totally untested, just to get the idea):

var funcarr = [];
function iterfuncs(arr){ 
   arr.pop()(); 
   if(arr.length >=1){ 
     iterfuncs(arr);
   } 
}

if you have a lot of functions in your array and worry about your call-stack you can change the recursive call to be called with a timeout of 0 which lets the calling function return before the next one is executed. poor mans tail-recursion.

window.setTimeout(function(){ iterfuncs(arr); },0); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.