AJAX回调非常慢

AJAX callback is super-slow?

本文关键字:非常 回调 AJAX      更新时间:2023-09-26

我正在尝试AJAX,并成功地部署了一个简单的AJAX a同步函数,但当我将其更改为使用回调方法时,突然间,加载需要很长时间(大约10-15分钟…)。以下是立即执行的功能:

function ajaxf() {
var xmlhttp;
  xmlhttp=new XMLHttpRequest();
  xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4 && xmlhttp.status==200 && document.getElementById("icons")==null)
    {
    document.getElementById("text-12").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","http://some-url/ajax.php",true);
  xmlhttp.send();
} 

下面是使用回调函数的较慢迭代方式:

function ajaxf(url,cfunc) {
  xmlhttp=new XMLHttpRequest();
  xmlhttp.onreadystatechange=cfunc;
  xmlhttp.open("GET",url,true);
  xmlhttp.send();
}
document.body.onscroll = function ajaxb() {
  ajaxf("http://some-url/ajax.php",function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200 && document.getElementById("icons")==null)
    {
    document.getElementById("text-4").innerHTML=xmlhttp.responseText;
    }
  });
} 

其他(可能)相关细节-ajax.php文件的重量仅为532 B,在我的本地测试服务器上,两者的运行或多或少相同,第一个函数在body标记中使用onscroll="ajaxf()"。。。

我觉得AJAX会更敏捷一点???

我解决了这个问题,多亏了@jasen的提示,我放了一个console.log(),并且能够像@jfriend00所说的那样看到滚动函数被放大了无数次。最初,我认为通过将"document.getElementById("icons")==null"作为条件,函数只会触发一次,但我当然错了,

所以解决方案是:在第一次执行后,通过在函数末尾添加"document.body.onscroll=null;"来重置onscroll操作。