I have a simple function to display a console message with an item name. Something simple like:
for(var i =0; i< array.length; i++)
child(array[i]);
var child = function(itemname){
console.log(itemname);
}
Here length of array is dynamic and so are the values it contains. Now I want to add some animation inside the child function and definitely want it to finish first before it gets called again by the for loop.
I know how to use jQuery Deferred and can call one function after finishing other but here I don't know how to call use it inside a for loop. I'm not sure if that's the right option to use here. So, what I want to do is,
for(var i =0; i< array.length; i++) //for example 3 times
{
child(i) //call child and pass the the value of i
wait till child function completes
}
So, with every increment in i, child function will be called and for loop should wait until child function is completed and then call child function again.... till the condition is met.
I found some solutions with $.when.apply functionality but couldn't figure our how to use it. Any documentation, sample, reference, 'to read' article will help!
EDIT: I guess, I shouldn't have used the example of animation. my bad. Assume that the child function makes an ajax call. Now, I want to call the child function i times and want the loop to wait every time to let the ajax call to complete before calling it again. It is the same function which I want to call more than once. no chaining.