AngularJS延迟(超时)循环中的每个http调用

时间:2016-07-28 20:13:03

标签: angularjs http timeout delay

我有一个数组。当我循环通过数组时,我撰写请求并进行http调用。但我希望每个http PUT请求在16秒内完成。我试过很多种方法。我尝试用$ timeout,$ interval包装http.then来循环1次,在http配置中添加超时:16000。他们都没有推迟http put调用。只有第一次在循环中调用才会延迟。如何将每个实际的http呼叫延迟16秒?这是我的代码。我在http配置中添加了超时以及$ timeout。我一次尝试一个,两个。没有工作

angular.forEach($scope.provisionDataArray, function(provReq, index) {
      var userProvisionSCIMUrl = someurl;

      scimProvReq = prepareProvisionRequestJSON(provReq, $scope.refData, $scope.App);
      var scimReq = {
        method: 'PUT',
        url: someurl,
        headers: {
          'Content-Type': 'application/json'
        },
        timeout: 16000,
        data: scimProvReq
      }
      $timeout(function() {
            $http(scimReq).then(function successCallback(response) {

                  var provStatus = {};
                  provStatus.reqNum = index;
                  provStatus.nbid = response.data.id;
                  provStatus.id = response.data.request.id;
                  provStatus.status = response.data.request.status;
                  provStatus.statusMessage = response.data.request.statusMessage;
                  $scope.provisionStatus.push(provStatus);

                },
                function errorCallback(response) {
                  $scope.errors.push({
                        error: "Error processing,
  line: index
    });                
    }); 
 },16000,$scope.provisionDataArray.length)
 }
});

3 个答案:

答案 0 :(得分:0)

使用递归策略,在前一个请求完成后调用每个请求。

var index = 0

function recursionRequests() {
  if (typeof $scope.provisionDataArray[index] == 'undefined') return; // breaking condition when reached our last request

  var provReq = $scope.provisionDataArray[index];

  var userProvisionSCIMUrl = someurl;

  scimProvReq = prepareProvisionRequestJSON(provReq, $scope.refData, $scope.App);

  var scimReq = {
    method: 'PUT',
    url: someurl,
    headers: {
      'Content-Type': 'application/json'
    },
    timeout: 16000,
    data: scimProvReq
  }

  $http(scimReq).then(function successCallback(response) {
      var provStatus = {};
      provStatus.reqNum = index;
      provStatus.nbid = response.data.id;
      provStatus.id = response.data.request.id;
      provStatus.status = response.data.request.status;
      provStatus.statusMessage = response.data.request.statusMessage;
      $scope.provisionStatus.push(provStatus);
    },
    function errorCallback(response) {
      $scope.errors.push({
        error: "Error processing",
        line: index
      });

    });

  $timeout(recursionRequests, 16000);
  index++;
}

更新:几乎忘记了超时XD

答案 1 :(得分:0)

你可以尝试类似的东西。

var scimReq = {
     method: 'PUT',
     url: someurl,
     headers: {
          'Content-Type' : 'application/json'
     },
     timeout: 16000,
     data: scimProvReq,
     index: 0
}

$scope.doIt = function() {
    if($scope.provisionDataArray.length > 0){
        $timeout(function(){
            $http(scimReq).then(function(){
                if(scimReq.index !== $scope.provisionDataArray.length){
                    scimReq.index = scimReq.index + 1;
                    $scope.doIt();
                }
            });
        }, scimReq.timeout);
    }
}

$scope.doIt();

$ interval每隔X毫秒执行一次。你的代码的问题是,间隔内唯一的部分是$ http调用,因此它会迭代并立即为每个项目调用$ interval($ http)。

答案 2 :(得分:0)

我用以下代码解决了这个问题。如果我打破循环来进行http调用我认为我可以实现延迟。所以我将我的数组切成1块的大小,然后添加超时。如果我将块大小设置为2,则在2次调用后添加延迟。

 var provisionChunks = sliceProvisionArray($scope.provisionDataArray, chunkSize);
 angular.forEach(provisionChunks, function(provReqs, index)  { 
    console.log("provReqs ,index ",provReqs + " " + index);
        angular.forEach(provReqs, function(provReq, index1)  { 
            setTimeout(function(x) { 
                return function() {  
                    provisionUsers(provReq, index1, provRequestCount);                              
                 }; 
               }(index), 12000*index);
            });
          });

 function sliceProvisionArray(arr, chunkSize) {
   var slicedProvArray = [], i;
      for (i = 0; i < arr.length; i += chunkSize) {
           slicedProvArray.push(arr.slice(i, i + chunkSize));
       }
       return slicedProvArray;
    }

function provisionUsers(provReq, index, provReqCount) {
   var userProvisionSCIMUrl =  "https://link"
     var scimProvReq = prepareProvisionRequestJSON(provReq, $scope.referenceData, $scope.virtualApp);  
       var scimReq = makePUTSCIMReq(scimProvReq,userProvisionSCIMUrl);
       $http(scimReq).then(function successCallback(response) {  

        }
     },
     function errorCallback(response) {                                                       
      }