Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.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_Xmlhttprequest - Fatal编程技术网

在JavaScript中,如何测试在给定时刻是否有任何AJAX调用在后台运行?

在JavaScript中,如何测试在给定时刻是否有任何AJAX调用在后台运行?,javascript,ajax,xmlhttprequest,Javascript,Ajax,Xmlhttprequest,我必须使用本机javascript(尽管如果我将jQuery解决方案转换为本机javascript,它也可以工作) 另外,我没有现有AJAX请求的句柄,因此无法直接访问它们 我正在搜索以下内容: var ajaxRequestsInProgress = document.getAllAjaxRunning(); ajaxRequestsInProgress[1].getStatus(); // will return the status of the request, for example

我必须使用本机javascript(尽管如果我将jQuery解决方案转换为本机javascript,它也可以工作)

另外,我没有现有AJAX请求的句柄,因此无法直接访问它们

我正在搜索以下内容:

var ajaxRequestsInProgress = document.getAllAjaxRunning();
ajaxRequestsInProgress[1].getStatus(); // will return the status of the request, for example

所以我可以访问这个对象并检查/操作现有的ajax请求。

我们可以说,这有点棘手。没有本地的方法可以做到这一点。因此,我们需要进行一些黑客攻击,修改本机
XMLHttpRequest
函数。大概是这样的:

var getAJAXRequests = (function() {
    var oldSend = XMLHttpRequest.prototype.send,
        currentRequests = [];

    XMLHttpRequest.prototype.send = function() {
        currentRequests.push(this); // add this request to the stack
        oldSend.apply(this, arguments); // run the original function

        // add an event listener to remove the object from the array
        // when the request is complete
        this.addEventListener('readystatechange', function() {
            var idx;

            if (this.readyState === XMLHttpRequest.DONE) {
                idx = currentRequests.indexOf(this);
                if (idx > -1) {
                    currentRequests.splice(idx, 1);
                }
            }
        }, false);
    };

    return function() {
        return currentRequests;
    }
}());
可以使用
getAJAXRequests()
调用它


您可以看到它的实际应用。

我知道您并没有特别要求提供
Mootools
解决方案,但我做了一些类似的事情,我想在这里分享给其他人参考。我通过重写各种请求方法来实现这一点。我目前使用的是1.5.1版,但是这个或类似的版本应该可以在其他版本上使用

(function() {
    var requestCounter = 0,
        send = Request.prototype.send,
        cancel = Request.prototype.cancel,
        onSuccess = Request.prototype.onSuccess,
        onFailure = Request.prototype.onFailure,
        timeout = Request.prototype.timeout;

    var increment = function() {
        ++requestCounter;
    };

    var decrement = function() {
        --requestCounter;

        if (requestCounter < 0) {
            requestCounter = 0;
        }
    };

    Request.implement({
        send: function() {
            increment();
            return send.apply(this, arguments);
        },
        cancel: function() {
            decrement();
            return cancel.apply(this, arguments);
        },
        onSuccess: function() {
            decrement();
            return onSuccess.apply(this, arguments);
        },
        onFailure: function() {
            decrement();
            return onFailure.apply(this, arguments);
        },
        timeout: function() {
            decrement();
            return timeout.apply(this, arguments);
        }
    });

    Request.getRunningCount = function() {
        return requestCounter;
    };
})();

非常好的方法!通过“重新定义”send()方法,您可以在底层工作,使您能够跟踪任何XMLHttpRequest,无论它是由哪个框架或插件发出的。将oldSend.apply行移到this.addEventListener之后是否有意义?似乎不可能在添加事件侦听器之前返回响应,但理论上可能吗?这并不能提供问题的答案。要评论或要求作者澄清,请在他们的帖子下方留下评论-你可以随时在自己的帖子上发表评论,一旦你有足够的评论,你就可以发表评论。我不同意我的回答有那么远-唯一的区别是它不是本机javascript。否则,它与上面的响应非常接近。另外,我来寻找答案,偶然看到了这篇文章,我想我会在未来为其他人提供一个类似但略有不同的选择。
var request1 = new Request(...);
var request2 = new Request(...);

request1.send();
request2.send();
console.log(Request.getRunningCount()); // 2

request1.cancel();
console.log(Request.getRunningCount()); // 1

// request2 completes
console.log(Request.getRunningCount()); // 0