Himmators
Himmators

Reputation: 15026

Why isn't my $timeout timeouting in angular?

I have a simple controller that is intended to make a simple count-down.

When I run this code, all I get is a single "tick" in the console. I would expect one tick every 5 milliseconds.

.controller('Countdown', function($scope, $timeout){
    $scope.endtime = new Date('May 5, 2014 00:00:00');
    var countup = function() {
        $scope.currtime = new Date();
        $scope.timeleft = $scope.endtime-$scope.currtime;
        console.log('tick');
    };
    $timeout(countup, 5);
});

Upvotes: 0

Views: 70

Answers (2)

Jay Shukla
Jay Shukla

Reputation: 5826

As Michael said you should use $interval

Or you can do like this.

.controller('Countdown', function($scope, $timeout){
    $scope.endtime = new Date('May 5, 2014 00:00:00');
    var countup = function() {
        $scope.currtime = new Date();
        $scope.timeleft = $scope.endtime-$scope.currtime;
        console.log('tick');
        $timeout(countup, 5); // Recursive technique
    };

});

Upvotes: 3

doodeec
doodeec

Reputation: 2927

$timeout is setTimeout wrapper, what you want is to set interval

Upvotes: 1

Related Questions