I have a piece of code like that, in a Angular (with Ionic) app:
my_app.factory('connectivityInterceptorService', ['$q', '$rootScope', function ($q, $rootScope) {
var connectivityInterceptorServiceFactory = {};
var _request = function (config) {
if(navigator.connection) {
if(navigator.connection.type == Connection.NONE) {
}
}
return config;
};
connectivityInterceptorServiceFactory.request = _request;
return connectivityInterceptorServiceFactory;
}])
navigator.connection can return the "type" (using a cordova plugin) of the connection (wifi, 3G, none, etc...). This code works perfect, I can detect whether the device has or not connection. The thing is: I want to, inside this if statement, "cancel" the request. But I can't figure out if there's possible.
As far as I know, if you're using good old $http to make the request, the 'timeout' property of the request can be attached to a promise and resolve when the promise is resolved:
Taken from the guide: (link below)
// The timeout property of the http request takes a deferred value
// that will abort the underying AJAX request if / when the deferred
// value is resolved.
So what you can do is let your connection function return a promise to the timeout of the $http and resolve it to a certain value to activate the timeout if there is no connection.
Link to guide: http://www.bennadel.com/blog/2616-aborting-ajax-requests-using-http-and-angularjs.htm
Hope it helps !