1

Is that possible to set delay between the javascript for loop NOT using only settimeout but based on when an specific ajax call is done?

something like:

for (var i = 0 ; i < list.length ; i++)
{
   $when('index.jsp').done(function(a1){
     alert(i);
   });
}

Say ajax request is sent and the first alert comes up, then the second iteration and alert is performed when the first ajax call is done.

0

2 Answers 2

3

I think the following should work:

(function() {
    var i = 0, end = list.length;
    function iterate() {
        $.ajax('index.jsp').done(function() {
            alert(i);
            i++;
            if (i < end) {
                iterate();
            }
        });
    }
    iterate();
})();
Sign up to request clarification or add additional context in comments.

3 Comments

not sure why it gives me syntax error! something wrong at the ending maybe?
I was missing a close brace, fixed.
Thanks, I am trying to test it but somehow the iterate is not triggered. should I call it or it gets triggered on load?
1

An easy way to accomplish what you're after would be to use a callback in the Ajax function to recursively call itself. Otherwise I'm not sure of a way to do it in a for loop because Ajax calls are asynchronous.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.