gpt4 book ai didi

javascript - addEventListener ("load",...) 导致过多的 XmlHttpRequest?

转载 作者:行者123 更新时间:2023-11-28 19:31:04 24 4
gpt4 key购买 nike

假设我在 Firefox 插件中运行以下 JavaScript 代码,该代码应在页面加载时执行 XmlHttpRequest:

function my_fun(){

var xmlHttpConnection = new XMLHttpRequest();
xmlHttpConnection.open('GET', globalUrl+'/call_this.php', true);
xmlHttpConnection.onreadystatechange=function(){
if (xmlHttpConnection.readyState == 4 && xmlHttpConnection.status == 200) {
var serverResponse = xmlHttpConnection.responseText;
do_stuff();
clearTimeout(xmlHttpTimeout);
xmlHttpConnection.abort();
}
};

xmlHttpConnection.send(null);
xmlHttpTimeout = setTimeout(function (){
do_other_stuff();
xmlHttpConnection.abort();
clearTimeout(xmlHttpTimeout);
},5000);
}

container.addEventListener("load", my_fun, true);

当调用 my_fun() 时,我的 Apache 服务器的访问日志如下所示:

<client-ip> - - [06/Nov/2014:10:40:04 -0500] "GET /call_this.php HTTP/1.1" 200 1 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0"

进一步假设我的服务器上有 4 个客户端连接。在 1 秒内,该行可能会同时在访问日志中出现数十次(有时超过 50 次),并且 ping 有时仅来自 1 个客户端。大多数情况下,每秒 ping 次数低于 5,但偶尔会出现由 call_this.php 引起的峰值。

为什么会发生这种情况?我怀疑 container.addEventListener("load", my_fun, true); 有问题。这是否会超出加载次数,例如,如果页面包含多个加载的隐藏 URL?如果是这样,我该如何解决这个问题? (编辑:观察员服务会更好吗?)

最佳答案

是的。我觉得有些不对劲:

检查这一行:

container.addEventListener("load", function(){my_fun();}, true);

或更改函数声明:

function my_fun(){
...

对于

my_fun = function(){
...

关于javascript - addEventListener ("load",...) 导致过多的 XmlHttpRequest?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26787722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com