gpt4 book ai didi

javascript - 当没有互联网连接时停止所有 future 调用的更好方法

转载 作者:行者123 更新时间:2023-11-28 20:05:46 25 4
gpt4 key购买 nike

当没有互联网连接时,我想停止所有 future 的 ajax 调用。我知道$rootScope.isOnline = navigator.onLine;用于检查任一用户是否在线/离线的线路。

我可以在 $httpProvider.interceptors 中添加任何内容,并停止所有调用并仅向用户显示一条警报消息,表明您未连接到互联网。

最佳答案

也许像这样:

var module = angular.module("app", []);
module.factory("ConnectivityService", function ($window) {
var service = {
connected: true
};
$window.addEventListener("offline", function(e) {
alert("offline");
service.connected = false;

});
$window.addEventListener("online", function(e) {
alert("online");
service.connected = true;

});
return service;
});
module.factory("ConnectedInterceptor", function ($q, ConnectivityService) {
return {
request: function (config) {
if (ConnectivityService.connected) {
return config || $q.when(config);
}
return $q.reject(config);
}
};
});
module.config(function ($httpProvider) {
$httpProvider.interceptors.push('ConnectedInterceptor');
});

尽管这个 navigator.onLine 相当不可靠:/

关于javascript - 当没有互联网连接时停止所有 future 调用的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20853257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com