开发者

JavaScript apparently waits for each AJAX call before sending another in a loop

开发者 https://www.devze.com 2023-01-03 11:19 出处:网络
Straight to the point: I have this javascript: for(item=1;item<5;item++) { xmlhttp=new XMLHttpRequest();

Straight to the point: I have this javascript:

for(item=1;item<5;item++)
{
    xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET",'zzz.php', true);
    xmlhttp.send();
}

And in PHP file something like this:

usleep(5);die('ok');

Now the problem is javascript seems to be waiting for each ajax call to be completed before sending another one. So the first response gets back after approx. 5 seconds, next after 10 seconds and so on.

That's a very simplified version of what I do, since the real script involves using cURL in PHP and jQuery as JS lib. But the problem r开发者_C百科emains the same.

Why do responses come back in 5 second intervals?


If you are using sessions within your PHP, then successive requests will be "held" until the lock on the session file is release, eithe by termination of the previous script holding the lock, or by an explicit session_write_close() call within that script. This gives the appearance of the server only running a single request at a time, and may be your problem.


All five requests will probably not be handled at the same time because the HTTP standard specifically says that you are only guaranteed to have two simultaneous connections to the server at any time (most browsers have a higher limit though, so they may try to have about 4-8 connections). If you hit a limit higher than one, then this is a probable bottleneck.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号