Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AJAX回调是超慢的吗?_Javascript_Ajax - Fatal编程技术网

Javascript AJAX回调是超慢的吗?

Javascript AJAX回调是超慢的吗?,javascript,ajax,Javascript,Ajax,我正在用AJAX进行一些试验,并成功地部署了一个简单的AJAX a-Synched函数,但当我将其更改为使用回调方法时——突然,加载需要花费很长时间(大约10-15分钟…)。 下面是立即执行的函数: function ajaxf() { var xmlhttp; xmlhttp=new XMLHttpRequest(); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlht

我正在用AJAX进行一些试验,并成功地部署了一个简单的AJAX a-Synched函数,但当我将其更改为使用回调方法时——突然,加载需要花费很长时间(大约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”作为一个条件,函数只会触发一次,但我当然错了

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

您正在启动滚动请求。在那里放一个
console.log()。速度缓慢可能是因为您正在启动大量ajax请求。下面是如何延迟滚动回调:感谢提示,伙计们,在您的帮助下,我能够解决它。。。为了将来参考,我将写下我的结论作为答案。