我写了一个函数来检测我的App是否有网络连接。使用IE11和Chrome时,当我停止无线时,状态会正确更改。但是使用Firefox,我总是获得200的状态,无论是否有无线。我的职责是:
private GetOffOnlineStatus(url: string, maxWaitTime: number, callback: (response: number) => void) {
var xhr = new XMLHttpRequest();
var noResponseTimer = setTimeout(function () {
xhr.abort();
if (callback) callback(xhr.status);
return;
}, maxWaitTime);
xhr.onreadystatechange = function (e) {
if (xhr.readyState === 4) {
clearTimeout(noResponseTimer);
if (callback) callback(xhr.status);
};
}
xhr.open("GET", url, true);
xhr.send(null);
}
我在构造函数中调用了这个函数(所以我在这个网站上找到了我用于测试的url:https://www.html5rocks.com/en/tutorials/cors/):
private urlCheckConn: string = "http://html5rocks-cors.s3-website-us-east-1.amazonaws.com/index.html";
private timeOutCheckConn: number = 1000;
private intervallCheckConn: number = 10000;
constructor() {
//Call when the page is open
setTimeout(this.GetOffOnlineStatus(this.urlCheckConn, this.intervallCheckConn, function (response) {
localStorage.setItem("CurrentStatusConnection", response.toString());
console.log(response.toString());
}), 100);
//Call every 10 seconds
setInterval(() => {
this.GetOffOnlineStatus(this.urlCheckConn, this.intervallCheckConn, function (response) {
localStorage.setItem("CurrentStatusConnection", response.toString());
console.log(response.toString());
}); }, this.intervallCheckConn);
}
显然原因是页面在缓存中。可以强制应用程序始终在网上获取文件吗?或者我必须使用其他方法? 提前谢谢。