How to execute Ajax requests several times at a time

I'm not sure if this is possible in JavaScript, so I thought I'd ask. :)

Let's say we have 100 queries that need to be completed, and we want to speed up the process.

What I was going to do is:

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

Given that the JavaScript browser does not support the stream, can we "use" the async functionality for this? Do you think this will work or have problems with this in JavaScript?

+7
javascript jquery
source share
3 answers

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

Currently, browsers restrict HTTP requests, so you can just start all 100 requests right away, and the browser will take care of sending these requests as quickly as possible, but with a limited number of concurrent requests.

So, start them right away and trust in the browser.

However, this may change in the future (the number of concurrent requests sent by the browser increases as the bandwidth of the Internet network of the end user increases and technology progresses).

EDIT : you should also think and read about the meaning of "asynchronous" in the javascript context .. asynchronous here means that you will give up control on something of some other parts of the system. therefore, "sending" an asynchronous request simply means that you tell the browser about it! you do not control the browser , just tell him to send this request and please tell me about the results .

+2
source share

Yes, I already did something like this. The main process:

  • Create a stack to store your tasks (requests in this case).
  • Start by completing 3 or 4 queries.
  • In the request callback, pull the next job from the stack and execute it (giving it the same callback).
+3
source share

It is actually slower to break 100 requests and batch messages every 5 times, waiting for them to complete until you send the next batch. You might be better off just sending 100 requests, remember that JavaScript is single-threaded, so it can only allow one response at a time at any time.

The best way is to configure a batch request service that accepts something like:

 /ajax_batch?req1=/some/request.json&req2=/other/request.json 

And so on. Basically you send multiple requests in a single HTTP request. The response of such a request will look like this:

 [ {"reqName":"req1","data":{}}, {"reqName":"req2","data":{}} ] 

Your ajax_batch service will resolve each request and return the results in the correct order. Customer, you track what you sent and what you expect so that you can match the results with the right queries. Downside, it requires quite some coding.

The speedup will be completely caused by the massive reduction in HTTP requests. There is a limit to the number of requests you send, as the length of the URL has an iirc limit.

DWR does exactly that afaik.

0
source share

All Articles