简体   繁体   English

HTTP请求持续使用angularjs的x秒时弹出警报

[英]popup alert when http request last x seconds with angularjs

I want to popup and alert saying "Error Contacting Server" when http request doesn't get any feedback. 当HTTP请求未获得任何反馈时,我想弹出并警告说“ Error Contacting Server”。

.controller('items_ctrl',['$scope','$http',function($scope,$http){
    $scope.shop_id=localStorage.getItem("shop_id");
        $http.get('http://localhost/myapp/spree/stocks.php').success(function(data){
            console.log(JSON.stringify(data));
            $scope.item=data;

           });
    }])

You should use then instead of success/error methods 您应该使用then而不是success/error方法

Here is your updated code 这是您更新的代码

.controller('items_ctrl',['$scope','$http',function($scope,$http){
    $scope.shop_id=localStorage.getItem("shop_id");
        $http.get('http://localhost/myapp/spree/stocks.php').then(function(data){
               console.log(JSON.stringify(data));
               $scope.item=data;

           }, function(error){
               alert("Error contacting server");
           });
        }])

You should specify error callback function. 您应该指定错误回调函数。

$http.get('/someUrl', config).then(successCallback, errorCallback);

For more information please see https://docs.angularjs.org/api/ng/service/ $http 有关更多信息,请参见https://docs.angularjs.org/api/ng/service/ $ http

Use promise for it .Set some timeout in your javascript. 为此使用诺言。请在JavaScript中设置一些超时时间。

var cancelReq= $q.defer();
$http.get('/someUrl', {timeout: cancelReq.promise}).success(successCallback);
$timeout(showErrorPopup, 3000);

after timeout , resolve it and then show popup 超时后,解决它,然后显示弹出窗口

function showErrorPopup(){
   cancelReq.resolve(); 
//popup code
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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