Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform Ajax requests, a few at a time

I am not really sure it is possible in JavaScript, so I thought I'd ask. :)

Say we have 100 requests to be done and want to speed things up.

What I was thinking of doing is:

  • Create a loop that will launch the first 5 ajax calls
  • Wait until they all return (success - call a function to update the dom / error) - not sure how, maybe with a global counter?
  • Repeat until all requests are done.

Considering browser JavaScript does not support thread, can we "exploit" the async functionality to do that? Do you think it would work, or there are inherent problems doing that in JavaScript?

like image 974
johnjohn Avatar asked Nov 21 '10 22:11

johnjohn


People also ask

How do I fire AJAX request periodically?

Use setInterval() when you want to send AJAX request at a particular interval every time and don't want to depend on the previous request is completed or not. But if you want to execute the AJAX when the previous one is completed then use the setTimeout() function.

Can we make multiple AJAX calls from a single page?

There is a requirement to make multiple AJAX calls parallelly to fetch the required data and each successive call depends on the data fetched in its prior call. Since AJAX is asynchronous, one cannot control the order of the calls to be executed.

Is there a limit to AJAX calls?

there's no limit to the ajax request itself, but serializing such an amount of data takes a while and requires quite some memory; so these are points where the browser may interfere with your code.

How many AJAX requests are there?

It is worth noting that browsers can generally only handle 6 ajax requests at a time, this may catch you out.


2 Answers

Yes, I have done something similar to this before. The basic process is:

  1. Create a stack to store your jobs (requests, in this case).
  2. Start out by executing 3 or 4 of the requests.
  3. In the callback of the request, pop the next job out of the stack and execute it (giving it the same callback).
like image 156
Chris Laplante Avatar answered Sep 29 '22 13:09

Chris Laplante


I'd say, the comment from Dancrumb is the "answer" to this question, but anyway...

Current browsers do limit HTTP requests, so you can even easily just start all 100 request immediately, and the browser will take care of sending those requests as fast as possible, but limited to a decent number of parallel requests.

So, just start them all immediately and trust on the browser.

However, this may change in the future (the number of parallel requests that a browser sends increases as end-user internet bandwidth increases and technology advances).

EDIT: you should also think and read about the meaning of "asynchronous" in a javascript context.. asynchronous here just means that you give up control about something to some other part of a system. so "sending" an async request just means, that you tell the browser to do so! you do not control the browser, you just tell it to send that request and please notify me about the outcome.

like image 27
Frunsi Avatar answered Sep 29 '22 14:09

Frunsi