AngularJs using to create a start and stop counting timer functionality
application. This application is need the following directive like $interval and $filter, We create default time object is display current timer clock functions.
A timer counter is have three functionality as below that start, stop and reset. A start function is called to start the $interval directive to active so now counter is begin, This moment you are unable to proceed a reset logic.
Stop function is is call to $interval belongs one of the method like cancel, This method make corresponding active interval prose is stop it. Reset function is call to reset all scope value is to be zero.
HTML:
<div ng-app="timerApp">
<div ng-controller="timerController">
Current Time : {{time}} <br/>
<hr/>
<Button ng-click="timer_start()">Start</Button>
<Button ng-click="timer_stop()">Stop</Button>
<Button ng-click="timer_reset()" ng-disabled="isStartStatus">Reset</Button>
<span>{{timerCount}}</span>
</div>
</div>
AngularJS:
var app = angular.module("timerApp", []);
app.controller("timerController",['$scope','$interval','$filter', function($scope, $interval, $filter){
$interval(function(){
$scope.time = $filter('date')(new Date(),"hh:mm:ss a", 'UTC')
},1000,0,null);
$scope.minutesleft = 0;
$scope.secondsleft = 0;
$scope.hoursleft = 0;
$scope.millisecondsleft = 0;
$scope.timerCount = $scope.hoursleft + ":" + $scope.minutesleft + ":" + $scope.secondsleft + ":" + $scope.millisecondsleft;
$scope.isStartStatus = true;
/**/
var promise;
$scope.timer_start = function(){
$scope.isStartStatus = true;
promise = $interval(function(){
$scope.millisecondsleft = $scope.millisecondsleft+1;
if($scope.millisecondsleft>100){
$scope.secondsleft = $scope.secondsleft+1;
$scope.millisecondsleft = 0;
if($scope.secondsleft>60){
$scope.minutesleft = $scope.minutesleft+1;
$scope.secondsleft = 0;
if($scope.minutesleft>60){
$scope.hoursleft = $scope.hoursleft+1;
$scope.minutesleft = 0;
if($scope.hoursleft==24){
$scope.hoursleft = 0;
}
}
}
}
$scope.timerCount = $scope.hoursleft + ":" + $scope.minutesleft + ":" + $scope.secondsleft + ":" + $scope.millisecondsleft;
},10,0,null);
};
$scope.timer_stop = function(){
$scope.isStartStatus = false;
$interval.cancel(promise);
}
$scope.timer_reset = function(){
$scope.minutesleft = 0;
$scope.secondsleft = 0;
$scope.hoursleft = 0;
$scope.millisecondsleft = 0;
$scope.timerCount = $scope.hoursleft + ":" + $scope.minutesleft + ":" + $scope.secondsleft + ":" + $scope.millisecondsleft;
}
}]);
Result:
Demo
Comments
Post a Comment