【问题标题】:AngularJS - How to make a stop watch starting from 00:00:00 formatAngularJS - 如何从 00:00:00 格式开始制作秒表
【发布时间】:2015-08-29 11:43:32
【问题描述】:

我想创建一个秒表。我用谷歌搜索并得到了一些关于如何制作计时器的提示。这是我在控制器中所做的:

$scope.value = 0;
$scope.startTimer = function() {

     $scope.value = 0;

    var change = function() {
        $scope.value += 1000;
        $timeout(change,1000);
    };
    $timeout(change, 1000);
}

并且在我的html中以这种格式显示值:

<label>{{value | date: 'hh:mm:ss'}}</label>

问题是时钟总是从 04:00:00 开始,当我调用 startTimer() 函数时,它可以启动计时器,但我希望计时器是这样的:

00:00:00

00:00:01

00:00:02

....

谁能告诉我如何在 00:00:00 开始计时器?

【问题讨论】:

    标签: angularjs datetime timer angular-filters


    【解决方案1】:

    已编辑:更新了带有间隔计时器的代码,因此现在代码包含带有 $timeout$interval 的两个计时器。
    来吧,制作你自己的过滤器:

    var app = angular.module("myApp",[]);
    app.controller("myController",function($scope, $timeout, $interval){
       
       //timer with timeout
       $scope.timerWithTimeout = 0;
       $scope.startTimerWithTimeout = function() {
        $scope.timerWithTimeout = 0;
        if($scope.myTimeout){
          $timeout.cancel($scope.myTimeout);
        }
        $scope.onTimeout = function(){
            $scope.timerWithTimeout++;
            $scope.myTimeout = $timeout($scope.onTimeout,1000);
        }
        $scope.myTimeout = $timeout($scope.onTimeout,1000);
      };
      
      $scope.resetTimerWithTimeout = function(){
        $scope.timerWithTimeout = 0;
        $timeout.cancel($scope.myTimeout);
      }
      
      //timer with interval
      $scope.timerWithInterval = 0;
       $scope.startTimerWithInterval = function() {
        $scope.timerWithInterval = 0;
        if($scope.myInterval){
          $interval.cancel($scope.myInterval);
        }
        $scope.onInterval = function(){
            $scope.timerWithInterval++;
        }
        $scope.myInterval = $interval($scope.onInterval,1000);
      };
      
      $scope.resetTimerWithInterval = function(){
        $scope.timerWithInterval = 0;
        $interval.cancel($scope.myInterval);
      }
    })
    app.filter('hhmmss', function () {
      return function (time) {
        var sec_num = parseInt(time, 10); // don't forget the second param
        var hours   = Math.floor(sec_num / 3600);
        var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
        var seconds = sec_num - (hours * 3600) - (minutes * 60);
    
        if (hours   < 10) {hours   = "0"+hours;}
        if (minutes < 10) {minutes = "0"+minutes;}
        if (seconds < 10) {seconds = "0"+seconds;}
        var time    = hours+':'+minutes+':'+seconds;
        return time;
      }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div ng-app="myApp" ng-controller="myController">
      <label>Timer with timeout: {{timerWithTimeout | hhmmss}}</label>
      <button ng-click="startTimerWithTimeout()">Start Timer</button>
      <button ng-click="resetTimerWithTimeout()">reset tiemr</button>
      <br><br>
      <label>Timer with interval: {{timerWithInterval | hhmmss}}</label>
      <button ng-click="startTimerWithInterval()">Start Timer</button>
      <button ng-click="resetTimerWithInterval()">reset tiemr</button>
    </div>

    补充:对于计时器,最好使用$interval 而不是$timeout。并始终将 $interval 引用到某个变量 :$scope.timer = $interval(change, 1000); ,因此您可以在每次重置时销毁它:$interval.cancel($scope.timer);

    【讨论】:

    • 谢谢!像魅力一样工作
    • @Sibtain ,我也做了一些更改,并添加了一些与 $timeout 和 $interval 使用相关的有用注释。
    • 谢谢。我可以使用 $timeout 来停止计时器吗?
    • 是的,你可以用同样的方式。 $scope.timer = $timeout(change,1000); 然后$interval.cancel($scope.timer);
    • @mudasserajaz 你能把代码改成使用 $interval 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多