Search code examples
angularjsdatetimetimerangular-filters

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


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.startTimer = function() {

     $scope.value = 0;

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

And in my html displayed value in this format:

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

The problem is the clock always starts at 04:00:00 and when I call the startTimer() function, it starts the timer okay but I want the timer to be like:

00:00:00

00:00:01

00:00:02

....

Can anyone tell me how to start timer at 00:00:00?


Solution

  • 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); .