0

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

2 Answers 2

3

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

});
Sign up to request clarification or add additional context in comments.

1 Comment

Works great, a little note to future internet travellers is that it doesn't work in older versions of angular!
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.