0

I am writing my own countdown timer ticker in Angular.js as a service. The value shows up in the element correctly, but it is not counting down. Not sure what I am doing wrong since I have the $interval(function(){ }, 1000); in the service to cause a digest() and make it tick.

angular.module('monitorApp')
.factory('countDown', function($interval) {
    $interval(function(){ }, 1000);
    return {
        countDownTicker: function(secondsLeft) {
            secondsLeft = Math.round(secondsLeft/1000);
            return --secondsLeft;
        }
    }
});

Controller:

$scope.countDownTicker = countDown.countDownTicker(result.broadcastStamp);

HTML:

 <span class="info-test">{{ countDownTicker }}</span>

UPDATE: New service...still not working:

angular.module('monitorApp')
.factory('countDown', function($interval) {
    $interval(function(){ }, 1000);
    return {
        countDownTicker: function(secondsLeft) {
            return $interval(function(secondsLeft){
                secondsLeft = Math.round(secondsLeft/1000);
                --secondsLeft;
                }, 1000);
        }
    }
});
5
  • 1
    not returning the interval part from factory, in fact it isn't wrapping anything. You need that interval to make the changes Commented Jul 7, 2014 at 2:01
  • Done, still not ticking down. Commented Jul 7, 2014 at 2:16
  • create a demo in jsfiddle or plunker Commented Jul 7, 2014 at 2:19
  • The interval should trigger a digest cycle every second. I suspect the issue is that seconds is not initialized. Commented Jul 7, 2014 at 3:28
  • Is it passing by value instead of reference and so therefore not updating? Commented Jul 7, 2014 at 3:34

1 Answer 1

1

I think you are using $interval in the wrong way. It returns a promise which will be notified on each iteration.

See code sample in angularJS's doc for reference.

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

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.