admin管理员组

文章数量:1323714

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?

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?

Share Improve this question edited Nov 21, 2010 at 23:26 Frunsi 7,1575 gold badges38 silver badges43 bronze badges asked Nov 21, 2010 at 22:43 johnjohnjohnjohn 4,2818 gold badges38 silver badges46 bronze badges 5
  • Added php tag, since it might be relevant for a good solution. – BGerrissen Commented Nov 21, 2010 at 23:17
  • BGerrissen: Ok, PHP is widely used for web server side scripting and such, but this question is absolutely not related to PHP! – Frunsi Commented Nov 21, 2010 at 23:25
  • @frunsi For Java DWR speeds up concurrent Ajax request quite nicely, there must be a PHP equivalent. So PHP can most definatly be relevant, the question is mostly "want to speed things up" and there might be other PHP developers that can help this PHP developer. – BGerrissen Commented Nov 21, 2010 at 23:34
  • Wait, I'm confused. Should every client side Ajax question be tagged with PHP then? – JJJ Commented Nov 21, 2010 at 23:40
  • @Juhana, Nah, in the ment thread of my awnser, PHP on the backend was mentioned ;) – BGerrissen Commented Nov 21, 2010 at 23:42
Add a ment  | 

3 Answers 3

Reset to default 3

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).

I'd say, the ment 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 oute.

It's actually slower to break up 100 requests and batch post them 5 at a time whilst waiting for them to plete till you send the next batch. You might be better off simply sending 100 requests, remember JavaScript is single threaded so it can only resolve 1 response at a time anyways.

A better way is set up 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 would look like:

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

Your ajax_batch service would resolve each request and send back the results in proper order. Client side, you keep track of what you sent and what you expect, so you can match up the results to the correct requests. Downside, it takes quite some coding.

The speed gain would e entirely from a massive reduction of HTTP requests. There's a limit on how many requests you send because the url length has a limit iirc.

DWR does exactly that afaik.

本文标签: javascriptHow to perform Ajax requestsa few at a timeStack Overflow