循环通过http调用

时间:2016-10-10 15:59:45

标签: javascript angularjs

我是angularjs的新手。我收到上一次http呼叫的响应后,需要重复进行http呼叫,并在获得http呼叫的响应后再添加一个x秒的延迟,然后再调用它。 我现在所拥有的是下面但我觉得这可以改善。

var TestCtrl= function ($scope, TestServiceFactory, $timeout) {

    var onTestError = function (reason) {
        $scope.TestData = null;
        getTestData();
    };

    var onTestSuccess = function (response) {
        $scope.TestData = response;
        getTestData();
    };

     var getTestData = function() {
       // Wait for 2 seconds after making the call again
        $timeout(function () {
            TestServiceFactory.getTestData().then(onTestSuccess, onTestError );
        }, 2000);
     };

    //First time service factory is called
    TestServiceFactory.getTestData().then(onTestSuccess, onTestError );
};
app.controller("TestCtrl", ['$scope', 'TestServiceFactory', '$timeout', TestCtrl]);

非常感谢任何建议。

0 个答案:

没有答案