views:

89

answers:

2

hello I'm developing an ajax project, but i'm confusing in something

i have 3 functions that are sending data to the server every function has a certain job.

is it better to group the ajax request that will be send from each function and send it in one big request. which will reduce my requests count & time. or send each request separately which will reduce execution time for my code at the server side but will make many requests.

P.S: for my application it works in both cases.

A: 

If you can consolidate them into a single request, neatly, I'd suggest you do that. Browsers will put restrictions on how many requests you can be making simultaneously (can be as low as 2), as will many servers.

Of course, as always, there are exceptions. If you have some data that you need to query every 10 seconds, and other data you need to query every 10 minutes, it's really not necessary to query the 10-minute data along with the 10-second data every 10 seconds.

Perhaps a single request with options that control what is sent back. You can request large amounts of data, or small amounts of data, all from the same request depending on the options you pass along with the request itself.

Just theory.

Jonathan Sampson
i'm really with this solution, but i found many famous ajax projects online that handle every request separately, although that can be handled all together
From.ME.to.YOU
Individual requests aren't necessarily bad. I just wanted to point out that you have other concerns when you go that route. Not knowing the specifics of your project, I can't offer much more than that. Best luck!
Jonathan Sampson
thanks or your help, i'm in the situation where i have a function that needs like 10 seconds to send a request and another that will be send like every 1 min, but i'm thinking in making a timeout for the 10 seconds function but if there is a request that will be send from the 1 min function it will take the 10 seconds data as well and reset its timer, what do you think. thanks for your help
From.ME.to.YOU
Unless you *need* to request them both at the same time, I don't see any harm in two distinct instances of `setInterval()` running in parallel.
Jonathan Sampson
+1  A: 

There are several factors involved here.

  • Putting them together on the client has the advantage of the client doing more of the work, which is good.
  • All but the newest browsers only allow 2 requests to the same domain at a time. If you have a lot of latency, this would cause to to want to group requests together if they're going to the same domain. However, newer browsers allow 8 requests at a time, so this problem will gradually go away.

But on the other hand

  • Do the requests need to all be made at the same time? Keeping the requests separate will allow you to maintain flexibility.
  • Probably the most important: Keeping the request separate will probably result in more maintainable, straightforward code.

I would suggest doing whatever keeps the code on the browser straightforward. Javascript (or frameworks that do ajax for you) is difficult enough to maintain and coordinate. Keep it simple!

Patrick Karcher