AngularJS - 如何制作从 00 :00:00 format 开始的秒表

标签 angularjs datetime timer angular-filters

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

$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 启动计时器?

最佳答案

已编辑:使用间隔计时器更新了代码,因此现在代码包含带有 $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.计时器); .

关于AngularJS - 如何制作从 00 :00:00 format 开始的秒表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32285679/

相关文章:

ruby-on-rails - 使用 AngularJS 从 Rails Asset Pipeline 显示图像的最佳方法?

node.js - 使用 Node 和 Express 构建 Angular 应用程序时是否有必要使用 JADE?

Angularjs - 最后一次 ng-repeat li 之后的意外空间

sql - 如何在 SQL Server 2008 中设置日期时间变量?

c - MSP430 Timer_A - 寄存器的比例

css - 如何在 ngRepeat 元素上使用 ngAnimate?

asp.net - mysql 将 varchar 列与日期(现在)进行比较,给出错误的结果。(我无法更改表。)

java - 我有一个日期对象,需要对其进行格式化以进行缓存查找,simpledateformat 不是线程安全的吗?

c++ - Boost basic_deadline_timer 在几次迭代后停止触发

由于多个进程导致的linux高分辨率posix定时器延迟