user6579134
user6579134

Reputation: 839

show an alert when http requesht has lasted for x seconds without response with angularjs

I want to show an alert when an http request has lasted for x seconds without return data which could be lack of connectivity

 $scope.loadmore = function() {
    $http.get('http://myweb.com/feeds.php?page='+$scope.page+).success(function(data) {
    console.log(JSON.stringify(data));
      for(var i=0;i<data.length-1;i++){
      $scope.posts.push(data[i]);
      }
      $scope.$broadcast('scroll.infiniteScrollComplete');
      //console.log($scope.page);
      $scope.page +=1;
    });
  };

Upvotes: 0

Views: 35

Answers (1)

Gerard Cuadras
Gerard Cuadras

Reputation: 1654

You can set a timeout function on your $http requests as shown in the following code, simply after the URL parameter:

 $scope.loadmore = function() {
    $http.get('http://myweb.com/feeds.php?page='+$scope.page+, {timeout: 3000})
        .success(function(data) {
            console.log(JSON.stringify(data));
            for(var i=0;i<data.length-1;i++){
                $scope.posts.push(data[i]);
            }
            $scope.$broadcast('scroll.infiniteScrollComplete');
            //console.log($scope.page);
            $scope.page +=1;
        })
        .error(function(error){
             //handle the error
        });
};

In this case the timeout is set to 3 seconds.

Upvotes: 1

Related Questions