Skip to content

Commit 6925f7f

Browse files
committed
setting countdownattr value on tick
1 parent 0400f27 commit 6925f7f

File tree

3 files changed

+135
-132
lines changed

3 files changed

+135
-132
lines changed

app/js/timer.js

Lines changed: 66 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ angular.module('timer', [])
1515

1616
// Checking for trim function since IE8 doesn't have it
1717
// If not a function, create tirm with RegEx to mimic native trim
18-
if(typeof String.prototype.trim !== 'function') {
19-
String.prototype.trim = function() {
18+
if (typeof String.prototype.trim !== 'function') {
19+
String.prototype.trim = function () {
2020
return this.replace(/^\s+|\s+$/g, '');
2121
};
2222
}
@@ -106,72 +106,73 @@ angular.module('timer', [])
106106

107107
function calculateTimeUnits() {
108108

109-
// compute time values based on maxTimeUnit specification
110-
if(!$scope.maxTimeUnit || $scope.maxTimeUnit === 'day') {
111-
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
112-
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
113-
$scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
114-
$scope.days = Math.floor((($scope.millis / (3600000)) / 24));
115-
$scope.months = 0;
116-
$scope.years = 0;
117-
} else if($scope.maxTimeUnit === 'second') {
118-
$scope.seconds = Math.floor($scope.millis / 1000);
119-
$scope.minutes = 0;
120-
$scope.hours = 0;
121-
$scope.days = 0;
122-
$scope.months = 0;
123-
$scope.years = 0;
124-
} else if($scope.maxTimeUnit === 'minute') {
125-
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
126-
$scope.minutes = Math.floor($scope.millis / 60000);
127-
$scope.hours = 0;
128-
$scope.days = 0;
129-
$scope.months = 0;
130-
$scope.years = 0;
131-
} else if($scope.maxTimeUnit === 'hour') {
132-
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
133-
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
134-
$scope.hours = Math.floor($scope.millis / 3600000);
135-
$scope.days = 0;
136-
$scope.months = 0;
137-
$scope.years = 0;
138-
} else if($scope.maxTimeUnit === 'month') {
139-
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
140-
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
141-
$scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
142-
$scope.days = Math.floor((($scope.millis / (3600000)) / 24) % 30);
143-
$scope.months = Math.floor((($scope.millis / (3600000)) / 24) / 30);
144-
$scope.years = 0;
145-
} else if($scope.maxTimeUnit === 'year') {
146-
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
147-
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
148-
$scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
149-
$scope.days = Math.floor((($scope.millis / (3600000)) / 24) % 30);
150-
$scope.months = Math.floor((($scope.millis / (3600000)) / 24 / 30) % 12);
151-
$scope.years = Math.floor(($scope.millis / (3600000)) / 24 / 365);
152-
}
153-
154-
// plural - singular unit decision
155-
$scope.secondsS = $scope.seconds==1 ? '' : 's';
156-
$scope.minutesS = $scope.minutes==1 ? '' : 's';
157-
$scope.hoursS = $scope.hours==1 ? '' : 's';
158-
$scope.daysS = $scope.days==1 ? '' : 's';
159-
$scope.monthsS = $scope.months==1 ? '' : 's';
160-
$scope.yearsS = $scope.years==1 ? '' : 's';
161-
//add leading zero if number is smaller than 10
162-
$scope.sseconds = $scope.seconds < 10 ? '0' + $scope.seconds : $scope.seconds;
163-
$scope.mminutes = $scope.minutes < 10 ? '0' + $scope.minutes : $scope.minutes;
164-
$scope.hhours = $scope.hours < 10 ? '0' + $scope.hours : $scope.hours;
165-
$scope.ddays = $scope.days < 10 ? '0' + $scope.days : $scope.days;
166-
$scope.mmonths = $scope.months < 10 ? '0' + $scope.months : $scope.months;
167-
$scope.yyears = $scope.years < 10 ? '0' + $scope.years : $scope.years;
109+
// compute time values based on maxTimeUnit specification
110+
if (!$scope.maxTimeUnit || $scope.maxTimeUnit === 'day') {
111+
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
112+
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
113+
$scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
114+
$scope.days = Math.floor((($scope.millis / (3600000)) / 24));
115+
$scope.months = 0;
116+
$scope.years = 0;
117+
} else if ($scope.maxTimeUnit === 'second') {
118+
$scope.seconds = Math.floor($scope.millis / 1000);
119+
$scope.minutes = 0;
120+
$scope.hours = 0;
121+
$scope.days = 0;
122+
$scope.months = 0;
123+
$scope.years = 0;
124+
} else if ($scope.maxTimeUnit === 'minute') {
125+
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
126+
$scope.minutes = Math.floor($scope.millis / 60000);
127+
$scope.hours = 0;
128+
$scope.days = 0;
129+
$scope.months = 0;
130+
$scope.years = 0;
131+
} else if ($scope.maxTimeUnit === 'hour') {
132+
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
133+
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
134+
$scope.hours = Math.floor($scope.millis / 3600000);
135+
$scope.days = 0;
136+
$scope.months = 0;
137+
$scope.years = 0;
138+
} else if ($scope.maxTimeUnit === 'month') {
139+
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
140+
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
141+
$scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
142+
$scope.days = Math.floor((($scope.millis / (3600000)) / 24) % 30);
143+
$scope.months = Math.floor((($scope.millis / (3600000)) / 24) / 30);
144+
$scope.years = 0;
145+
} else if ($scope.maxTimeUnit === 'year') {
146+
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
147+
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
148+
$scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
149+
$scope.days = Math.floor((($scope.millis / (3600000)) / 24) % 30);
150+
$scope.months = Math.floor((($scope.millis / (3600000)) / 24 / 30) % 12);
151+
$scope.years = Math.floor(($scope.millis / (3600000)) / 24 / 365);
152+
}
153+
154+
// plural - singular unit decision
155+
$scope.secondsS = $scope.seconds == 1 ? '' : 's';
156+
$scope.minutesS = $scope.minutes == 1 ? '' : 's';
157+
$scope.hoursS = $scope.hours == 1 ? '' : 's';
158+
$scope.daysS = $scope.days == 1 ? '' : 's';
159+
$scope.monthsS = $scope.months == 1 ? '' : 's';
160+
$scope.yearsS = $scope.years == 1 ? '' : 's';
161+
//add leading zero if number is smaller than 10
162+
$scope.sseconds = $scope.seconds < 10 ? '0' + $scope.seconds : $scope.seconds;
163+
$scope.mminutes = $scope.minutes < 10 ? '0' + $scope.minutes : $scope.minutes;
164+
$scope.hhours = $scope.hours < 10 ? '0' + $scope.hours : $scope.hours;
165+
$scope.ddays = $scope.days < 10 ? '0' + $scope.days : $scope.days;
166+
$scope.mmonths = $scope.months < 10 ? '0' + $scope.months : $scope.months;
167+
$scope.yyears = $scope.years < 10 ? '0' + $scope.years : $scope.years;
168168

169169
}
170+
170171
//determine initial values of time units and add AddSeconds functionality
171172
if ($scope.countdownattr) {
172173
$scope.millis = $scope.countdownattr * 1000;
173174

174-
$scope.addCDSeconds = $element[0].addCDSeconds = function(extraSeconds){
175+
$scope.addCDSeconds = $element[0].addCDSeconds = function (extraSeconds) {
175176
$scope.countdown += extraSeconds;
176177
$scope.$digest();
177178
if (!$scope.isRunning) {
@@ -180,12 +181,12 @@ angular.module('timer', [])
180181
};
181182

182183
$scope.$on('timer-add-cd-seconds', function (e, extraSeconds) {
183-
$timeout(function (){
184+
$timeout(function () {
184185
$scope.addCDSeconds(extraSeconds);
185186
});
186187
});
187188

188-
$scope.$on('timer-set-countdown-seconds', function(e, countdownSeconds) {
189+
$scope.$on('timer-set-countdown-seconds', function (e, countdownSeconds) {
189190
if (!$scope.isRunning) {
190191
$scope.clear();
191192
}
@@ -232,6 +233,7 @@ angular.module('timer', [])
232233

233234
if ($scope.countdown > 0) {
234235
$scope.countdown--;
236+
$scope.countdownattr = $scope.countdown.toString();
235237
}
236238
else if ($scope.countdown <= 0) {
237239
$scope.stop();

dist/angular-timer.js

Lines changed: 67 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* angular-timer - v1.1.2 - 2014-05-16 9:27 PM
2+
* angular-timer - v1.1.2 - 2014-05-19 11:03 PM
33
* https://github.com/siddii/angular-timer
44
*
55
* Copyright (c) 2014 Siddique Hameed
@@ -22,8 +22,8 @@ angular.module('timer', [])
2222

2323
// Checking for trim function since IE8 doesn't have it
2424
// If not a function, create tirm with RegEx to mimic native trim
25-
if(typeof String.prototype.trim !== 'function') {
26-
String.prototype.trim = function() {
25+
if (typeof String.prototype.trim !== 'function') {
26+
String.prototype.trim = function () {
2727
return this.replace(/^\s+|\s+$/g, '');
2828
};
2929
}
@@ -113,72 +113,73 @@ angular.module('timer', [])
113113

114114
function calculateTimeUnits() {
115115

116-
// compute time values based on maxTimeUnit specification
117-
if(!$scope.maxTimeUnit || $scope.maxTimeUnit === 'day') {
118-
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
119-
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
120-
$scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
121-
$scope.days = Math.floor((($scope.millis / (3600000)) / 24));
122-
$scope.months = 0;
123-
$scope.years = 0;
124-
} else if($scope.maxTimeUnit === 'second') {
125-
$scope.seconds = Math.floor($scope.millis / 1000);
126-
$scope.minutes = 0;
127-
$scope.hours = 0;
128-
$scope.days = 0;
129-
$scope.months = 0;
130-
$scope.years = 0;
131-
} else if($scope.maxTimeUnit === 'minute') {
132-
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
133-
$scope.minutes = Math.floor($scope.millis / 60000);
134-
$scope.hours = 0;
135-
$scope.days = 0;
136-
$scope.months = 0;
137-
$scope.years = 0;
138-
} else if($scope.maxTimeUnit === 'hour') {
139-
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
140-
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
141-
$scope.hours = Math.floor($scope.millis / 3600000);
142-
$scope.days = 0;
143-
$scope.months = 0;
144-
$scope.years = 0;
145-
} else if($scope.maxTimeUnit === 'month') {
146-
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
147-
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
148-
$scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
149-
$scope.days = Math.floor((($scope.millis / (3600000)) / 24) % 30);
150-
$scope.months = Math.floor((($scope.millis / (3600000)) / 24) / 30);
151-
$scope.years = 0;
152-
} else if($scope.maxTimeUnit === 'year') {
153-
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
154-
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
155-
$scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
156-
$scope.days = Math.floor((($scope.millis / (3600000)) / 24) % 30);
157-
$scope.months = Math.floor((($scope.millis / (3600000)) / 24 / 30) % 12);
158-
$scope.years = Math.floor(($scope.millis / (3600000)) / 24 / 365);
159-
}
160-
161-
// plural - singular unit decision
162-
$scope.secondsS = $scope.seconds==1 ? '' : 's';
163-
$scope.minutesS = $scope.minutes==1 ? '' : 's';
164-
$scope.hoursS = $scope.hours==1 ? '' : 's';
165-
$scope.daysS = $scope.days==1 ? '' : 's';
166-
$scope.monthsS = $scope.months==1 ? '' : 's';
167-
$scope.yearsS = $scope.years==1 ? '' : 's';
168-
//add leading zero if number is smaller than 10
169-
$scope.sseconds = $scope.seconds < 10 ? '0' + $scope.seconds : $scope.seconds;
170-
$scope.mminutes = $scope.minutes < 10 ? '0' + $scope.minutes : $scope.minutes;
171-
$scope.hhours = $scope.hours < 10 ? '0' + $scope.hours : $scope.hours;
172-
$scope.ddays = $scope.days < 10 ? '0' + $scope.days : $scope.days;
173-
$scope.mmonths = $scope.months < 10 ? '0' + $scope.months : $scope.months;
174-
$scope.yyears = $scope.years < 10 ? '0' + $scope.years : $scope.years;
116+
// compute time values based on maxTimeUnit specification
117+
if (!$scope.maxTimeUnit || $scope.maxTimeUnit === 'day') {
118+
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
119+
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
120+
$scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
121+
$scope.days = Math.floor((($scope.millis / (3600000)) / 24));
122+
$scope.months = 0;
123+
$scope.years = 0;
124+
} else if ($scope.maxTimeUnit === 'second') {
125+
$scope.seconds = Math.floor($scope.millis / 1000);
126+
$scope.minutes = 0;
127+
$scope.hours = 0;
128+
$scope.days = 0;
129+
$scope.months = 0;
130+
$scope.years = 0;
131+
} else if ($scope.maxTimeUnit === 'minute') {
132+
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
133+
$scope.minutes = Math.floor($scope.millis / 60000);
134+
$scope.hours = 0;
135+
$scope.days = 0;
136+
$scope.months = 0;
137+
$scope.years = 0;
138+
} else if ($scope.maxTimeUnit === 'hour') {
139+
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
140+
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
141+
$scope.hours = Math.floor($scope.millis / 3600000);
142+
$scope.days = 0;
143+
$scope.months = 0;
144+
$scope.years = 0;
145+
} else if ($scope.maxTimeUnit === 'month') {
146+
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
147+
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
148+
$scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
149+
$scope.days = Math.floor((($scope.millis / (3600000)) / 24) % 30);
150+
$scope.months = Math.floor((($scope.millis / (3600000)) / 24) / 30);
151+
$scope.years = 0;
152+
} else if ($scope.maxTimeUnit === 'year') {
153+
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
154+
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
155+
$scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
156+
$scope.days = Math.floor((($scope.millis / (3600000)) / 24) % 30);
157+
$scope.months = Math.floor((($scope.millis / (3600000)) / 24 / 30) % 12);
158+
$scope.years = Math.floor(($scope.millis / (3600000)) / 24 / 365);
159+
}
160+
161+
// plural - singular unit decision
162+
$scope.secondsS = $scope.seconds == 1 ? '' : 's';
163+
$scope.minutesS = $scope.minutes == 1 ? '' : 's';
164+
$scope.hoursS = $scope.hours == 1 ? '' : 's';
165+
$scope.daysS = $scope.days == 1 ? '' : 's';
166+
$scope.monthsS = $scope.months == 1 ? '' : 's';
167+
$scope.yearsS = $scope.years == 1 ? '' : 's';
168+
//add leading zero if number is smaller than 10
169+
$scope.sseconds = $scope.seconds < 10 ? '0' + $scope.seconds : $scope.seconds;
170+
$scope.mminutes = $scope.minutes < 10 ? '0' + $scope.minutes : $scope.minutes;
171+
$scope.hhours = $scope.hours < 10 ? '0' + $scope.hours : $scope.hours;
172+
$scope.ddays = $scope.days < 10 ? '0' + $scope.days : $scope.days;
173+
$scope.mmonths = $scope.months < 10 ? '0' + $scope.months : $scope.months;
174+
$scope.yyears = $scope.years < 10 ? '0' + $scope.years : $scope.years;
175175

176176
}
177+
177178
//determine initial values of time units and add AddSeconds functionality
178179
if ($scope.countdownattr) {
179180
$scope.millis = $scope.countdownattr * 1000;
180181

181-
$scope.addCDSeconds = $element[0].addCDSeconds = function(extraSeconds){
182+
$scope.addCDSeconds = $element[0].addCDSeconds = function (extraSeconds) {
182183
$scope.countdown += extraSeconds;
183184
$scope.$digest();
184185
if (!$scope.isRunning) {
@@ -187,12 +188,12 @@ angular.module('timer', [])
187188
};
188189

189190
$scope.$on('timer-add-cd-seconds', function (e, extraSeconds) {
190-
$timeout(function (){
191+
$timeout(function () {
191192
$scope.addCDSeconds(extraSeconds);
192193
});
193194
});
194195

195-
$scope.$on('timer-set-countdown-seconds', function(e, countdownSeconds) {
196+
$scope.$on('timer-set-countdown-seconds', function (e, countdownSeconds) {
196197
if (!$scope.isRunning) {
197198
$scope.clear();
198199
}
@@ -239,7 +240,7 @@ angular.module('timer', [])
239240

240241
if ($scope.countdown > 0) {
241242
$scope.countdown--;
242-
$scope.countdownattr = String($scope.countdown);
243+
$scope.countdownattr = $scope.countdown.toString();
243244
}
244245
else if ($scope.countdown <= 0) {
245246
$scope.stop();

0 commit comments

Comments
 (0)