0

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);
            };
         });
};

1 Answer 1

1

$timeout service takes first parameter as function and second parameter the number of milliseconds to wait to execute that function.

When you use $timeout($scope.checkChallengeAnswerd(challengeTarget), 2000), you are not passing the function into the $timeout service, instead you are passing the return value of the function.

Using $timeout(function() { $scope.checkChallengeAnswerd(challengeTarget); }, 2000) works fine as you are passing in function to $timeout service.

Other option is to modify the $scope.checkChallengeAnswerd(challengeTarget) function expression as:

$scope.checkChallengeAnswerd = function (challengeTarget) {
    return function () {
        $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);
            };
        });
    };
};
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! But if I pass the result of function, it should be an Error. Instead, it works, but performance too often.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.