2017-10-16 33 views
1

我在我的ionic1应用程序中的控制器中进行HTTP调用。有时互联网连接速度很慢,我想暂停脚本。例如,当存在20秒无响应警报应该弹出为ionic1中的http请求设置超时值angularjs

.controller('account_statement_ctrl', function($scope, $http, $ionicLoading, $ionicPopup, $cordovaToast, $location, $ionicModal, $filter) { 
    $scope.account_number = localStorage.getItem("account_number"); 
    ///alert if connection fails 
    $scope.connect = function() { 
    var alertPopup = $ionicPopup.alert({ 
     title: 'Error', 
     template: '<p align="center">Internet Connectivity Problem</p>', 
    }); 
    }; 


    $scope.nobill = function() { 
    var alertPopup = $ionicPopup.alert({ 
     title: 'Error', 
     template: '<p align="center">Not Found</p>', 
    }); 
    }; 

    $scope.acc_request = function() { 

    $scope.start_date = $filter('date')($scope.sdate, "yyyy-MM-dd"); 
    $scope.end_date = $filter('date')($scope.edate, "yyyy-MM-dd"); 

    $ionicLoading.show({ 
     template: '<p>Processing Request</p><ion-spinner></ion-spinner>' 
    }); 
    $http.get("http://localhost/app/state?account_number=" + $scope.account_number).success(function(data) { 
     $scope.statement = data.data 
     //console.log(JSON.stringify(data.data)) 
     { 
      $ionicLoading.hide(); 
     } 
    }) 
    } 
}) 

回答

0

$http.get返回Promise其可以调用一个或success回调error。您可以设置请求的超时时间(请参阅docs),然后对error回调中的超时作出反应。

$http.get(myUrl, { timeout: 20000 }).then(successCallback, errorCallback); 

阅读general documentation了解更多详情。

顺便说一句,您正在使用的脚本.success已弃用。 建议的方法是使用标准化的Promise API,即.then()

+0

你能把它写成我的脚本吗? – user6579134

+0

我添加了一个基本片段 –