問(wèn)題描述
有沒(méi)有一種簡(jiǎn)單的方法可以檢測(cè) XMLHttpRequest
在瀏覽器窗口中是否處于活動(dòng)狀態(tài)?或者有多少是活躍的?IE.有沒(méi)有辦法檢測(cè)我的瀏覽器窗口中是否有任何 AJAX 調(diào)用處于活動(dòng)狀態(tài)?
Is there an easy way to detect if an XMLHttpRequest
is active in the browser window? Or how many are active? ie. Is there a way to detect if there are any AJAX calls active in my browser window?
問(wèn)題擴(kuò)展: 使用 javascript 有沒(méi)有辦法可以查看是否打開(kāi)了任何 XMLHttpRequests?比如window.XMLisActive()"
之類的?
Extension of question: Using javascript is there a way I can see if any XMLHttpRequests are open? Such as "window.XMLisActive()"
or something like that?
解決方案:最終為 XMLHttpRequest
編寫(xiě)了一個(gè)包裝器:gist here
Solution: Ended up writing a wrapper for XMLHttpRequest
: gist here
推薦答案
除非您為 XmlHttpRequest
編寫(xiě)一個(gè)包裝器(或?qū)ζ溥M(jìn)行猴子補(bǔ)丁),否則無(wú)法檢測(cè)來(lái)自 JS 的打開(kāi)連接跟蹤打開(kāi)的連接.
There is not a way to detect an open connection from JS unless you write a wrapper for XmlHttpRequest
(or monkey patch it) that keeps track of opened connections.
這里是kidcapital的猴子補(bǔ)丁,不確定是否完美,但這是一個(gè)好的開(kāi)始
Here's kidcapital's monkey patch, not sure if it's perfect, but it's a good start
(function() {
var oldOpen = XMLHttpRequest.prototype.open;
window.openHTTPs = 0;
XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
window.openHTTPs++;
this.addEventListener("readystatechange", function() {
if(this.readyState == 4) {
window.openHTTPs--;
}
}, false);
oldOpen.call(this, method, url, async, user, pass);
}
})();
注意
這不處理 fetch
或 websockets,但你可以在這些情況下做類似的事情.
This does not handle fetch
or websockets but you could do something similar in those cases.
這篇關(guān)于如何檢查 HTTP 請(qǐng)求是否在瀏覽器中打開(kāi)?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!