Skip to content

Commit 29c4d3d

Browse files
committed
Added leading zero
Added an optional feature that adds a leading zero to a number if it is smaller than 10. Updated docs as well.
1 parent a4fbd4d commit 29c4d3d

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

app/js/timer.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ angular.module('timer', [])
88
startTimeAttr: '=startTime',
99
endTimeAttr: '=endTime',
1010
countdownattr: '=countdown',
11+
1112
autoStart: '&autoStart'
1213
},
1314
controller: ['$scope', '$element', '$attrs', '$timeout', function ($scope, $element, $attrs, $timeout) {
@@ -17,6 +18,8 @@ angular.module('timer', [])
1718
//backward and forward compatibility.
1819
$scope.autoStart = $attrs.autoStart || $attrs.autostart;
1920

21+
$scope.addLeadingZero = ($attrs.$attr.addLeadingZero || $attrs.$attr.addleadingzero ) ? true : false;
22+
2023
if ($element.html().trim().length === 0) {
2124
$element.append($compile('<span>{{millis}}</span>')($scope));
2225
} else {
@@ -95,12 +98,18 @@ angular.module('timer', [])
9598
});
9699

97100
function calculateTimeUnits() {
98-
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
99-
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
100-
$scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
101-
$scope.days = Math.floor((($scope.millis / (3600000)) / 24));
101+
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
102+
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
103+
$scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
104+
$scope.days = Math.floor((($scope.millis / (3600000)) / 24));
105+
106+
if($scope.addLeadingZero){
107+
$scope.seconds = $scope.seconds < 10 ? '0' + $scope.seconds : $scope.seconds;
108+
$scope.minutes = $scope.minutes < 10 ? '0' + $scope.minutes : $scope.minutes;
109+
$scope.hours = $scope.hours < 10 ? '0' + $scope.hours : $scope.hours;
110+
$scope.days = $scope.days < 10 ? '0' + $scope.days : $scope.days;
111+
}
102112
}
103-
104113
//determine initial values of time units and add AddSeconds functionality
105114
if ($scope.countdownattr) {
106115
$scope.millis = $scope.countdownattr * 1000;

dist/angular-timer.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* angular-timer - v1.0.12 - 2014-02-10 9:05 AM
2+
* angular-timer - v1.0.12 - 2014-03-17 9:06 PM
33
* https://github.com/siddii/angular-timer
44
*
55
* Copyright (c) 2014 Siddique Hameed
@@ -15,6 +15,7 @@ angular.module('timer', [])
1515
startTimeAttr: '=startTime',
1616
endTimeAttr: '=endTime',
1717
countdownattr: '=countdown',
18+
1819
autoStart: '&autoStart'
1920
},
2021
controller: ['$scope', '$element', '$attrs', '$timeout', function ($scope, $element, $attrs, $timeout) {
@@ -24,6 +25,8 @@ angular.module('timer', [])
2425
//backward and forward compatibility.
2526
$scope.autoStart = $attrs.autoStart || $attrs.autostart;
2627

28+
$scope.addLeadingZero = ($attrs.$attr.addLeadingZero || $attrs.$attr.addleadingzero ) ? true : false;
29+
2730
if ($element.html().trim().length === 0) {
2831
$element.append($compile('<span>{{millis}}</span>')($scope));
2932
} else {
@@ -102,12 +105,18 @@ angular.module('timer', [])
102105
});
103106

104107
function calculateTimeUnits() {
105-
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
106-
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
107-
$scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
108-
$scope.days = Math.floor((($scope.millis / (3600000)) / 24));
108+
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
109+
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
110+
$scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
111+
$scope.days = Math.floor((($scope.millis / (3600000)) / 24));
112+
113+
if($scope.addLeadingZero){
114+
$scope.seconds = $scope.seconds < 10 ? '0' + $scope.seconds : $scope.seconds;
115+
$scope.minutes = $scope.minutes < 10 ? '0' + $scope.minutes : $scope.minutes;
116+
$scope.hours = $scope.hours < 10 ? '0' + $scope.hours : $scope.hours;
117+
$scope.days = $scope.days < 10 ? '0' + $scope.days : $scope.days;
118+
}
109119
}
110-
111120
//determine initial values of time units and add AddSeconds functionality
112121
if ($scope.countdownattr) {
113122
$scope.millis = $scope.countdownattr * 1000;

dist/angular-timer.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<script src="bower_components/jquery/jquery.min.js"></script>
1212
<script src="bower_components/angular/angular.min.js"></script>
1313
<script src="bower_components/bootstrap/docs/assets/js/bootstrap.min.js"></script>
14-
<script src="dist/angular-timer.min.js"></script>
14+
<script src="app/js/timer.js"></script>
1515
<script src="docs/docs.js"></script>
1616
</head>
1717
<body ng-app="timer-demo" ng-controller="TimerDemoController">
@@ -64,6 +64,21 @@ <h3>
6464
<button class="btn" onclick="stopResumeTimer('clock-timer', this)" type="button">Stop</button>
6565
</div>
6666
</section>
67+
<section id="clock-timer-leading-zero">
68+
<h3>
69+
Timer with leading zero</h3>
70+
71+
<div class="bs-docs-example">
72+
<p>
73+
This markup <code ng-non-bindable="">&lt;timer interval=&quot;1000&quot; add-leading-zero&gt;{{hours}} hours, {{minutes}}
74+
minutes, {{seconds}} seconds.&lt;/timer&gt;</code> will run the clock timer ticking every second with an additional zero at the beginning if unit is smaller than 10</p>
75+
76+
<h3>
77+
<timer interval="1000" add-leading-zero>{{hours}} hours, {{minutes}} minutes, {{seconds}} seconds.</timer>
78+
</h3>
79+
<button class="btn" onclick="stopResumeTimer('clock-timer', this)" type="button">Stop</button>
80+
</div>
81+
</section>
6782
<section id="timer-with-start-time">
6883
<h3>
6984
Timer initialised with some predefined start time.</h3>

0 commit comments

Comments
 (0)