Angular.js倒计时定时器不滴答

Angular.js Count Down Timer not Ticking

本文关键字:滴答 定时器 倒计时 js Angular      更新时间:2023-09-26

我正在用Angular.js编写自己的倒计时计时器。该值正确地显示在元素中,但没有倒数。不确定我做错了什么,因为我在服务中有$interval(function(){ }, 1000);来引起digest()并使其勾选。

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

控制器:

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

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

更新:新服务…仍然不能工作:

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

我认为你使用$interval的方式不对。它返回一个promise,在每次迭代时都会被通知。