weixin_33690963 2019-06-30 12:45 采纳率: 0%
浏览 546

关闭特定的XMLHttpRequest

How to "rebuild" function to close specific XMLHttpRequest? I have defined variable outside function to call xhr.abort(); everywhere I need. Now is possible, with this solution, close last running XMLHttpRequest if running more than one at same time - processes before last running are without control after replace xhr by re-calling _ajax()

var xhr;

function _ajax(data, callback) {
  xhr = new XMLHttpRequest();
  xhr.open('POST', window.location.pathname, true);
  xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  xhr.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      callback(this);
    }
  };
  xhr.send(data);
}

/* close fnc */
xhr.abort();
  • 写回答

1条回答 默认 最新

  • weixin_33720452 2019-06-30 13:29
    关注

    You could use xhr as an array and store there all the requests; then you can call abort on any one of them. Like:

    var xhr=[];
    function _ajax(data, callback) {
        xhr.push(new XMLHpptRequest);
        //etc
    }
    xhr[0].abort();
    xhr.shift(); //get rid of the aborted request
    
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序