JavaScript Loop and wait for function

Posted by Fluidbyte on Stack Overflow See other posts from Stack Overflow or by Fluidbyte
Published on 2012-09-10T21:04:40Z Indexed on 2012/09/10 21:38 UTC
Read the original article Hit count: 246

Filed under:
|
|
|
|

I have a simple single-dimension array, let's say:

fruits = ["apples","bananas","oranges","peaches","plums"];

I can loop thru with with $.each() function:

$.each(fruits, function(index, fruit) {  
   showFruit(fruit);
});

but I'm calling to another function which I need to finish before moving on to the next item.

So, if I have a function like this:

function showFruit(fruit){
    $.getScript('some/script.js',function(){
        // Do stuff
    })
}

What's the best way to make sure the previous fruit has been appended before moving on?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery