Angularjs $ interval returns fn is not a function

I want to check if a cookie exists with an interval of $. I press $ interval to load the page. This call periodically throws an error:

> TypeError: fn is not a function
>     at callback (angular.js:12516)
>     at Scope.$eval (angular.js:17444)
>     at Scope.$digest (angular.js:17257)
>     at Scope.$apply (angular.js:17552)
>     at tick (angular.js:12506)

I really don't understand why.

Here is my code:

angular.module("appModule")
.controller("loginController", ["$scope", "$http", "$window", "$document", "$interval", "$cookies",
    function ($scope, $http, $window, $document, $interval, $cookies) {

    var stopInterval;
    $scope.CheckLoginCookie = function () {

        if ($cookies.get("Login") != null) {

            if (angular.isDefined(stopInterval)) {
                $interval.cancel(stopInterval);
                stopInterval = undefined;
            }

            $window.location.href = $scope.UrlNotes;
        }
    }

    $scope.Repeat = function ()
    {
        stopInterval = $interval($scope.CheckLoginCookie(), 1000);
    }
}]);

The code is called from $ document.ready:

$document.ready(function () {      
    $scope.Repeat();
})
+4
source share
2 answers

You added the result of the function instead of the function itself. The call $scope.CheckLoginCookie()will return undefined, but is $intervalwaiting for a callback.

$interval($scope.CheckLoginCookie, 1000);

If a function requires parameters, just use it like this:

$interval(function() {
    $scope.CheckLoginCookie(param1, param2);
}, 1000);
+10
source

Str , . 5 , . refreshChart, . $scope.Repeat . 5 , yay!

    $scope.Repeat = function () {
        intervalPromise = $interval(function () {
            $scope.refreshChart();
        }, 300000);
    }

    $scope.refreshChart();

    $scope.Repeat();
0

Source: https://habr.com/ru/post/1648492/


All Articles