3

I am facing an issue in Ionic . I am trying to abort my $http post and get request after 20 seconds forcefully and want to execute the http call error block which says "server issue found" .Is there is any way to abort http call and execute its error block forcefully after some seconds in ionic or cordova .Thanks in AdvanceIs there is any kind of parameter passed in a call to abort it after that much of seconds

user3454799
  • 93
  • 3
  • 8
  • 2
    Possible duplicate of [Angular $http : setting a promise on the 'timeout' config](http://stackoverflow.com/questions/21915834/angular-http-setting-a-promise-on-the-timeout-config) – e666 Aug 16 '16 at 08:03

2 Answers2

1

You can use timeout option in http request. If it reach the timeout limit, automatically it will execute the error block

 $http.get('path/to/service', {timeout: 5000});
Naresh Kumar
  • 938
  • 5
  • 12
1

Here is the correct syntax for timeout.

$http({
        method: POST,
        url: 'path/to/service',
        timeout: 5000
     }).success(function(data){
        // With the data succesfully returned, call our callback
        successFunc(data);
    }).error(function(){
        errorFunc("error");
    });
 }
});

Or modify the default timeout of http provider similar to following.

angular.module('MyApp', [])
  .config(['$httpProvider', function($httpProvider) {
    $httpProvider.defaults.timeout = 5000;
}]);

Goodluck.

Dinesh Devkota
  • 1,417
  • 2
  • 18
  • 45