JavaScript 显然会等待每个 AJAX 调用,然后再循环发送另一个调用

发布于 2024-09-06 07:46:39 字数 453 浏览 3 评论 0原文

开门见山: 我有这个 javascript:

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

在 PHP 文件中是这样的:

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

现在的问题是 javascript 似乎正在等待每个 ajax 调用完成,然后再发送另一个 ajax 调用。 所以第一个响应会在大约之后返回。 5 秒,10 秒后下一个,依此类推。

这是我所做的一个非常简化的版本,因为真正的脚本涉及在 PHP 和 jQuery 中使用 cURL 作为 JS 库。但问题仍然是一样的。

为什么响应每隔 5 秒返回一次?

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 remains the same.

Why do responses come back in 5 second intervals?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残花月 2024-09-13 07:46:39

如果您在 PHP 中使用会话,则连续的请求将被“保留”,直到会话文件上的锁被释放为止,或者通过终止持有锁的前一个脚本,或者通过在该脚本中显式调用 session_write_close() 。这使得服务器看起来一次只运行一个请求,这可能是您的问题。

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.

初见你 2024-09-13 07:46:39

所有五个请求可能不会同时处理,因为 HTTP 标准明确规定,您只能保证在任何时候都有两个同时连接到服务器(尽管大多数浏览器都有更高的限制,因此它们可能会尝试大约4-8 个连接)。如果您达到的限制高于 1,那么这可能是一个瓶颈。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文