简体   繁体   中英

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;
    });
  };

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM