Emil
Emil

Reputation: 1045

multiple ajax requests with jquery

I got problems with the async nature of Javascript / JQuery.

Lets say the following (no latency is counted for, in order to not make it so troublesome);

I got three buttons (A, B, C) on a page, each of the buttons adds an item into a shopping cart with one ajax-request each. If I put an intentional delay of 5 seconds in the serverside script (PHP) and pushes the buttons with 1 second apart, I want the result to be the following:

Request A, 5 seconds
Request B, 6 seconds
Request C, 7 seconds

However, the result is like this

Request A, 5 seconds
Request B, 10 seconds
Request C, 15 seconds

This have to mean that the requests are queued and not run simultaneously, right? Isnt this opposite to what async is? I also tried to add a random get-parameter to the url in order to force some uniqueness to the request, no luck though.

I did read a little about this. If you avoid using the same "request object (?)" this problem wont occure. Is it possible to force this behaviour in JQuery?

This is the code that I am using

 $.ajax(
 {
  url   : strAjaxUrl + '?random=' + Math.floor(Math.random()*9999999999),
  data  : 'ajax=add-to-cart&product=' + product,
  type  : 'GET',
  success  : function(responseData)
  {
   // update ui
  },
  error  : function(responseData)
  {
   // show error
  }
 });

I also tried both GET and POST, no difference.

I want the requests to be sent right when the button is clicked, not when the previous request is finnished. I want the requests to be run simultaneously, not in a queue.

Upvotes: 10

Views: 25685

Answers (5)

Emil
Emil

Reputation: 1045

Apparently this behaviour is very easily passable by using session_write_close(); in PHP. However, this opens up for DDos attacks with ease. I refreshed a page by holding down F5 in 10 seconds and killed my developmen computer in no-time.

Case closed.

Upvotes: 3

Amir Raminfar
Amir Raminfar

Reputation: 34150

Ok here is what I would try, I think you need a better way to log the time. I am not sure what you are doing now to test the times but here is a good way.

Get Firebug for Firefox if you haven't already done so.

Open Firebug and go to the net tab. Make sure you enable it.

As you click each button you should see each request coming in. If you click each button, you don't see the next request starting until the previous request has ended then this tells me there is something wrong in javascript. However, I am guessing you should the beginning of each bar to be 1 second apart. Do this and let us know what you see.

Edit

I think I found you problem. I am guessing you are using sleep function in php. The sleep function sleeps for the current session. If you open up 3 tabs, and put the ajax url, you will notice the same behavior. 5, 10, then 15s for each tab to finish. I tried googling and found the same results with other people. This is probably because PHP is not really multithreaded. I suggest using a different framework.

Upvotes: 8

Vitalii Ivanov
Vitalii Ivanov

Reputation: 3251

you cant do it simultaneously, cause JS have fake async operations due to one browser UI thread. But you can try Web Workers, the way to execute code outsude of the browser UI thread. They already supported in Firefox 3.5, Chrome 3, and Safari 4. Maybe it will improve your application usability

Upvotes: 0

rcravens
rcravens

Reputation: 8390

There is a limit on the number of concurrent Ajax requests. See this

How many concurrent AJAX (XmlHttpRequest) requests are allowed in popular browsers?

Looks like it is browser dependent.

Bob

Upvotes: 0

regilero
regilero

Reputation: 30496

From what I understand Parallel ajax calls can be done only if you're not in different scope of js. Seems that being in different same js scope makes you using the same 'request object' in a serialize way.

So we can find on Stack overflow two excellent response on this at the page linked, see the second response with a parallel query stack object, this is the thing you need, add your requests on this parallele stack and they will really get fired in parallel (Stack overflow is the best site in the web :-) ). Parallel asynchronous Ajax requests using jQuery

Upvotes: 0

Related Questions