How to get current time from Angular Timer

狂风中的少年 提交于 2019-12-23 20:32:07

问题


I am testing the Angular Timer and have found myself wondering about how to get the current time inside my controller in order to use it to whatever purpose I might have.

For instance, I want to set the font color of the timer to red when it reaches a specifict amount of minutes, but I was completely unable to do this.

I tried the following:

$scope.startTimer = function (deadline) {
    $scope.$broadcast('timer-start');
    $scope.timerRunning = true;
    if ($scope.minutes == deadline)
        $scope.turnRed();
};

This is the complete code: jsFiddle.

I also tried separatedly to just change the color in some other event calling the turnRed() function and for some weird reason it didn't work either.

I am sorry in advance that I wasn't able to add the angular timer library to the fiddle cause I am really new to it... If you could also help with this point I would really appreciate it!


回答1:


You can use timer-tick event here

$scope.$on('timer-tick', function (event, data) {
    if ($scope.timerRunning === true && data.millis >= $scope.deadlineInMilli) {
        $scope.$apply($scope.turnRed);
    }
});

And you need to convert your minutes to milliseconds for comparison

$scope.startTimer = function (deadline) {
    ....
    $scope.deadlineInMilli = +deadline * 1000 * 60; // converting to milliseconds
};

See the DEMO



来源:https://stackoverflow.com/questions/26950645/how-to-get-current-time-from-angular-timer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!