AngularJS - How to make a stop watch starting from 00:00:00 format

前端 未结 1 1118
囚心锁ツ
囚心锁ツ 2020-12-19 14:40

I want to create a stop watch. I googled and got a few tips on how to make a timer. Here is what I have done in my controller:

$scope.value = 0;
$scope.start         


        
相关标签:
1条回答
  • 2020-12-19 14:54

    EDITED: Updated code with interval timer, so now code contains both timers with $timeout and $interval.
    Here you go, make your own filter:

    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>

    Addition: For timer it is better to use $interval instead of $timeout. and always reference $interval to some variable :$scope.timer = $interval(change, 1000); , so you can destroy it on each reset:$interval.cancel($scope.timer); .

    0 讨论(0)
提交回复
热议问题