0

I have a for loop in java script :

for(i=1;i<=10;i++){
    $.ajax({
        type: "GET",
        url: "insert_names.php",
        success: function (data) {
            // some codes
        }
    });
}

this script send all of ajax request Simultaneously !

But It makes problem for me ...

how can i can prevent continue for loop when ajax is working ?

7
  • 4
    Welcome to the wonderful world of async! Use promises. Commented Dec 12, 2014 at 19:33
  • Aha, it seems I asked a very similar question a couple years ago. Lemme find it. Here you go: stackoverflow.com/questions/9952242/… Commented Dec 12, 2014 at 19:34
  • "how can i can prevent continue for loop when ajax is working" - Don't. Learning how to use the tools properly is going to be a lot easier than trying to bend them to what you think they should do. Commented Dec 12, 2014 at 19:37
  • @SLaks I'm trying to think, though, how he would do that on a loop. Commented Dec 12, 2014 at 19:37
  • @philtune: Do what in a loop? Send an AJAX request? The code already does that. The OP hasn't really made it clear why this causes a problem for him. Commented Dec 12, 2014 at 19:39

1 Answer 1

2

If you need the requests to be executed in serial instead of parallel, you can do that with a little re-structuring. Don't make the AJAX requests synchronous, just use the response of each request to invoke the next one.

Consider a structure like this:

function sendRequest() {
    $.ajax({
        type: "GET",
        url: "insert_names.php",
        success: function (data) {
            // some codes
        }
    });
}

Now the request is wrapped in a function that you can invoke. So invoke it in response to the previous request:

function sendRequest() {
    $.ajax({
        type: "GET",
        url: "insert_names.php",
        success: function (data) {
            // some codes
            sendRequest();
        }
    });
}

It's starting to look like a standard recursive pattern, so all you need now is a terminating condition. Similar to the original loop, just use an incrementing value:

var i = 1;

function sendRequest() {
    $.ajax({
        type: "GET",
        url: "insert_names.php",
        success: function (data) {
            // some codes
            i++;
            if (i <= 10) {
                sendRequest();
            }
        }
    });
}

Then just invoke it once to start it:

sendRequest();

Now the request should be invoked 10 times as expected (unless my math is off by one, but that should be easy to correct), each request happening in response to the previous request.

With asynchronous programming, don't try to make it synchronous. Instead, perform actions in response to the callbacks.

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

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.