如何从角定时器获取当前时间 [英] How to get current time from Angular Timer

查看:199
本文介绍了如何从角定时器获取当前时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试了 角定时器 并发现自己想知道如何得到当前的时间我的控制器内,以便用它来我可能拥有的任何目的。

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.

比如说,我要的设置定时器以红色的字体颜色,当它到达分钟的specifict量,但我完全无法做到这一点。

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.

我试过如下:

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

这是一个完整的code:的jsfiddle

This is the complete code: jsFiddle.

我也试过separatedly只是改变颜色在其他一些情况下调用 turnRed()功能和一些奇怪的原因,它也不能工作。

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.

我事先抱歉,我无法的角定时器库添加到小提琴的,因为我真的很新的给它......如果你也有这点我会帮助真的AP preciate呢!

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!

推荐答案

您可以使用这里定时器蜱事件

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

查看 DEMO

这篇关于如何从角定时器获取当前时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆