This is similar to the "answered" question
JQuery AJAX request behaving synchronous for unknown reasonI believe that the actual issue with parallel execution observed by MGM has been overlooked and actually there is a problem with $.get/$.ajax executed in parallel.
Look at the snippet below:
$(function() {
$(".dd").each(function() {
var obj = $(this);
$.get("test.txt", function(data) {
obj.html(data);
});
});
});
It loads a file (asynchronously I would expect) and displays it (synchronously of course).
However, the code executes differently during the first page load and on page refresh. I am monitoring the requests to the server using firebug Net panel in Firefox 4.0 on Windows. On the first page load (or when refreshed using Ctrl-F5) I can see on the Net panel that multiple requests to the "test.txt" start at the same time and the Net acivity mostly overlapps.
This part works as expected. The result may be processed in the browser one by one, but the requests to the server are performed in parallel.
It is completly different story when user presses 开发者_如何转开发F5 to refresh the page. Suddenly parallelism is gone. And same web page loads "test.txt" one by one.
It becomes even more clear if I replace data display (obj.html(data);) with a simple alert: alert(data); On initial page load I get multiple alert messages on the screen at the same time. Subsequent refreshes (F5) clearly demonstrate that while one alert message is on screen no other downloads are performed (I can remove the file to see next "$.get" to fail).
So in fact the $.get does not perform async. calls to the server.
Any ideas on why this happening?
P.S. Sorry system does not allow me to post images use provided URL to see the screenshots.
After various testing I tend to agree that $.get/$.ajax works asynchronous as supposed to.
The same code works perfectly asynchronous, provided the server does not respond immediatly but takes some time to process the requests. With artificial delay on the server, the logs collected on the server show that the requests are coming in parallel regardless if it is initial page load or subsequent refreshes. Firebug shows the same: the calls to server are overlapping.
The shorter requests are seem to be too quick and the browser "decides" to process the response before initiating the next call.
As has been noted, asynchronous does not mean to be exactly in parallel.
精彩评论