0

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 ?

Mastermind
  • 69
  • 2
  • 11
  • [**Asynchronous**](http://stackoverflow.com/questions/16336367/what-is-the-difference-between-synchronous-and-asynchronous-programming-in-node) JavaScript And XML – Liam Aug 04 '16 at 14:30

2 Answers2

2

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.

VoteyDisciple
  • 37,319
  • 5
  • 97
  • 97
  • You mean the http server is not answering to more than one request at a time ? That's definitely not my case :) I saw that there are ways to make concurrent ajax calls ... – Mastermind Aug 04 '16 at 14:30
  • Keep in mind the following thing: if you call getData() twice and the first request is not finished you'll have to wait until the first call is finished ... that's my issue ... and I don't call the function twice or ten times in a rows, I call it once, I wait 5 s then I'm calling again and so on ... – Mastermind Aug 04 '16 at 14:34
  • If you call `getData()` twice and the first request is not finished, you ***absolutely do not*** have to wait for the first call to finish. That simply isn't how AJAX behaves. The second request will be initiated ***immediately*** with any regard whatsoever for what any other in-progress HTTP requests are doing. – VoteyDisciple Aug 04 '16 at 14:37
  • Yes, the second request is initiated immediately but it will be sent to the server only after the first request has finished :) – Mastermind Aug 04 '16 at 15:10
0

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.