Mastermind
Mastermind

Reputation: 79

Ajax Concurrent Call Running One after Another

I have written a simple web page where I would like to be able to execute concurrent ajax requests. I know that I can do concurrent ajax requests in jquery using .when() but that's not exactly my case. I have a function like the following:

 function getData(tt, tf) {
$.ajax({
    url : "/extpage.php",
    type : "POST",
    async: true, 
    data : {
        testt : tt,
        testf : tf
    }
})
.done(function (toolbox) {
    alert(data);
});

}

This function is called from a button inside the webpage and I need to be able to let the user call this function anytime he wants (I'm aware about the maximum number of the ajax requests that a browser can support) without waiting the previous ajax request to be finished first and then execute the next one. I want every call to be processed in parallel. Any clues on how I can obtain that ?

Upvotes: 0

Views: 300

Answers (2)

Jake Whelan
Jake Whelan

Reputation: 64

I think you may need to revise your question.

This function is called from a button inside the webpage and I need to be able to let the user call this function anytime he want

This is the default AJAX behaviour. AJAX calls are ansychronous.

async: true

is redundant, true is the default value for async.

Your code should do what you are asking in this question, if you are still experiencing a problem the issue may be elsewhere.

As one last note:

$.when()

is used to queue otherwise concurrent/async tasks, the opposite of what you suggested in the OP.

Upvotes: 0

VoteyDisciple
VoteyDisciple

Reputation: 37813

That's how AJAX works inherently. Each call you perform is run independent of any other browser activity (including, generally, other AJAX calls).

Given the function you have, if I call getData() ten times in a row, it will initiate ten independent HTTP requests. If they're not running concurrently it is possible that the server simply won't answer more than one request at a time, and of course you can't do anything about that.

Upvotes: 2

Related Questions