循环api调用 - 性能缓慢

时间:2016-04-29 13:18:41

标签: javascript angularjs

我在一个循环中有一个API调用需要很长时间才能完成,但是如果我在循环中有一个短暂的延迟它会更快完成,这对我来说似乎很奇怪。

比下面的时间长了很多。

var questionsLength = questions.length; 
for (var i = 0; i < questionsLength; i++) {
  $scope.removeAll(questions[i]);
}

比上述表现更快。

var questionIndex = 0;
(function loop (i) {
  if (questionIndex === 0) {
    timeoutcount = 0;
  } else { 
    timeoutcount = 3000;
    }

  setTimeout(function () {
    $scope.removeAll(questions[questionIndex]);
    questionIndex++;
    if (--i) {          
      loop(i);       
    }
  }, timeoutcount);
})(questions.length);

处理多个API调用的适当方法是什么?

0 个答案:

没有答案