0

我正在使用以下代码进行多个异步。电话,我需要知道有多少电话正在等待验证。

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");

}

有办法知道吗?谢谢 (:

4

1 回答 1

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);
于 2014-05-26T20:27:21.847 回答