How to get current time with Angular Timer

I am testing Angular Timer and wondering how to get the current time inside my controller so that I can use it for any of my purposes.

For example, I want to set the timer font color to red when it reaches a certain number of minutes , but I was completely unable to do so.

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 it separately to just change the color in another event calling the function turnRed()

and for some strange reason it didn't work either.

I regret in advance that I was not able to add the angular timer library to the script because I am really new to this ... If you could help with this, I would really appreciate it!

+3


source to share


1 answer


You can use the timer 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 DEMO

+3


source







All Articles