我试图重复ajax调用脉冲,但是使用间隔有时因为网络问题我在队列中有几个ajax调用。我如何运行ajax调用,但只有在第一个调用成功后才运行下一个调用。另外,如果需要,我想取消通话。使用interval这很容易,因为我使用clearInterval。
$scope.intervalLoop = $interval(function() {
if ($scope.weAreClear)
$scope.initSoftwareUpdate();
}, 5000);
答案 0 :(得分:2)
如前所述,您可以使用返回$timeout
的递归函数:
$scope.cancelled = false;
$scope.recTimeoutFunction = function($scope) {
return $timeout(function($scope) {
//only start new pulse if user hasn't cancelled
if(!$scope.cancelled)
{
$http(/* pulsing ajax call */)
.then(function(res) {
//after successful ajax call, start new timeout:
$scope.recTimeoutFunction($scope);
})
}
}, 5000, true, $scope);
};
//initially call the timeout function:
$scope.recTimeoutFunction($scope);
//function for the user to cancel pulsing messages
$scope.cancel = function() {
$scope.cancelled = true;
}