I am trying to run loop with angular $timeout. Here is the thing: When I try to use this $timeout usage. I get nearly 15 requests per second instead of planned 1 per 2 second:
$timeout($scope.checkChallengeAnswerd(challengeTarget), 2000);
But everything is OK if I do something like that:
$timeout(function() { $scope.checkChallengeAnswerd(challengeTarget); }, 2000);
Could anybody explain why does this happen, please?
Here is full function code block:
$scope.checkChallengeAnswerd = function (challengeTarget) {
$http({
method: 'post',
url: CHESS_URL + "/challenge/check_answerd/",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data: { "target":challengeTarget }
}).success(function (data, status, headers, config) {
$scope.answerd = data.answerd;
if ($scope.answerd == "wait") {
//alert("wait");
$timeout(function() { $scope.checkChallengeAnswerd(challengeTarget); }, 2000);
}else{
$("div.b111").hide();
alert($scope.answerd);
};
});
};