【问题标题】:AngularJs timeout for long response timeAngularJs 超时响应时间长
【发布时间】:2013-09-04 10:27:41
【问题描述】:

如果后端调用需要更长的时间,我会创建一个显示消息的函数...

我试图观察 $locationChangeSuccess 事件......

        $scope.$on('$locationChangeStart', function (event, newLoc, oldLoc){
           deferred = $timeout(function() {
                    alert('takes more that 10 sec!!!');
                }, 10000);
        });


    $scope.$on('$locationChangeSuccess', function (event, newLoc, oldLoc){
           console.log('Cancelled: ' + $timeout.cancel(deferred));
        });

它不起作用,因为 $locationChangeSuccess 事件在 $location.path(...) 之后立即触发(没有等待响应)

您知道响应返回后立即触发的任何事件吗?

谢谢

【问题讨论】:

  • 你想在需要很长时间时取消还是只显示一条消息?

标签: angularjs time error-handling timeout request


【解决方案1】:

如果我理解正确的话:

app.controller('LocalCtrl', function ($scope, $timeout, $q) {

    var defer = $q.defer();

    defer.promise.then(function () {
        alert('End request.');
    });

    $timeout(function () {
        defer.resolve();
    }, 2000);

});

或者在你的路由配置中使用:

app.config(function ($routeProvider) {
    $routeProvider
        .when('/', {
              templateUrl: 'views/main.html'
            , controller: 'MainCtrl'
        })
        .when('/test', {
              templateUrl: 'views/local.html'
            , controller: 'LocalCtrl'
            , resolve: {
                app: function ($q, $timeout) {
                    var defer = $q.defer();
                    $timeout(function () {
                        defer.resolve();
                    }, 2000);

                    return defer.promise;
                }
            }
        })
        .otherwise({
            redirectTo: '/'
        });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-13
    • 2021-07-04
    • 2016-10-24
    • 2016-07-20
    • 2018-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多