Javascript 如何知道有多少异步调用挂起

Javascript 如何知道有多少异步调用挂起,javascript,sendasynchronousrequest,Javascript,Sendasynchronousrequest,我使用下面的代码来实现多个异步。我需要知道这些调用中有多少在等待验证 function llenarComboMetodos(cell) { var xhr; if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else if (window.ActiveXObject) { xhr = new ActiveXObject("Msxml2.XMLHTTP");

我使用下面的代码来实现多个异步。我需要知道这些调用中有多少在等待验证

function llenarComboMetodos(cell) {
    var xhr;
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        xhr = new ActiveXObject("Msxml2.XMLHTTP");
    }
    else {
        throw new Error("Las llamandas asincronas no son soportadas por este navegador.");
    }
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status == 200 && xhr.status < 300) {
                var combo = '<select name="metodos[]">';
                var opciones=xhr.responseText;
                combo+= opciones+"</select>";
                cell.innerHTML = combo;
            }
        }
    }

    xhr.open('POST', 'includes/get_metodos.php');
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send("completar=1");

}
函数LlenacomboMetodos(单元){
var-xhr;
if(window.XMLHttpRequest){
xhr=newXMLHttpRequest();
}
else if(window.ActiveXObject){
xhr=新的ActiveXObject(“Msxml2.XMLHTTP”);
}
否则{
抛出新错误(“Las llamandas asincronas no son soportadas por este navegador.”);
}
xhr.onreadystatechange=函数(){
if(xhr.readyState==4){
如果(xhr.status==200&&xhr.status<300){
var组合=“”;
var opciones=xhr.responseText;
组合+=选项+“”;
cell.innerHTML=组合;
}
}
}
open('POST','includes/get_metodos.php');
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
xhr.send(“completar=1”);
}
有办法知道吗?
谢谢(:

您可以截取
XMLHttpRequest.send
并对活动呼叫进行计数:

var activeXhr = (function(){
    var count = 0;
    XMLHttpRequest.prototype.nativeSend = XMLHttpRequest.prototype.send;
    XMLHttpRequest.prototype.send = function(body) {

        this.onreadystatechange  = function(){
            switch(this.readyState){
                case 2: count++; break
                case 4: count--; break
            }
        };
        this.nativeSend(body);
    };

    return count;
})();

console.log(activeXhr);

您需要手动维护。当然,在XHR状态1中开始计算它们,并在状态4中删除它们。添加一个计算它们的变量。完成后进行减法。