Skip to content

Commit

Permalink
addCDSeconds method work even for stopped countdown timers
Browse files Browse the repository at this point in the history
  • Loading branch information
siddii committed Feb 10, 2014
1 parent 42d932b commit a4fbd4d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
9 changes: 6 additions & 3 deletions app/js/timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ angular.module('timer', [])
$scope.start = $element[0].start = function () {
$scope.startTime = $scope.startTimeAttr ? new Date($scope.startTimeAttr) : new Date();
$scope.endTime = $scope.endTimeAttr ? new Date($scope.endTimeAttr) : null;
$scope.countdown = $scope.countdownattr && parseInt($scope.countdownattr, 10) > 0 ? parseInt($scope.countdownattr, 10) : undefined;
if (!$scope.countdown) {
$scope.countdown = $scope.countdownattr && parseInt($scope.countdownattr, 10) > 0 ? parseInt($scope.countdownattr, 10) : undefined;
}
resetTimeout();
tick();
$scope.isRunning = true;
Expand Down Expand Up @@ -106,6 +108,9 @@ angular.module('timer', [])
$scope.addCDSeconds = $element[0].addCDSeconds = function(extraSeconds){
$scope.countdown += extraSeconds;
$scope.$digest();
if (!$scope.isRunning) {
$scope.start();
}
};

$scope.$on('timer-add-cd-seconds', function (e, extraSeconds) {
Expand Down Expand Up @@ -154,9 +159,7 @@ angular.module('timer', [])
}
else if ($scope.countdown <= 0) {
$scope.stop();
return;
}

};

if ($scope.autoStart === undefined || $scope.autoStart === true) {
Expand Down
27 changes: 18 additions & 9 deletions dist/angular-timer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* angular-timer - v1.0.12 - 2014-02-03 9:30 PM
* angular-timer - v1.0.12 - 2014-02-10 9:05 AM
* https://github.com/siddii/angular-timer
*
* Copyright (c) 2014 Siddique Hameed
Expand Down Expand Up @@ -52,6 +52,10 @@ angular.module('timer', [])
$scope.clear();
});

$scope.$on('timer-set-countdown', function (e, countdown) {
$scope.countdown = countdown;
});

function resetTimeout() {
if ($scope.timeoutId) {
clearTimeout($scope.timeoutId);
Expand All @@ -61,7 +65,9 @@ angular.module('timer', [])
$scope.start = $element[0].start = function () {
$scope.startTime = $scope.startTimeAttr ? new Date($scope.startTimeAttr) : new Date();
$scope.endTime = $scope.endTimeAttr ? new Date($scope.endTimeAttr) : null;
$scope.countdown = $scope.countdownattr && parseInt($scope.countdownattr, 10) > 0 ? parseInt($scope.countdownattr, 10) : undefined;
if (!$scope.countdown) {
$scope.countdown = $scope.countdownattr && parseInt($scope.countdownattr, 10) > 0 ? parseInt($scope.countdownattr, 10) : undefined;
}
resetTimeout();
tick();
$scope.isRunning = true;
Expand Down Expand Up @@ -109,6 +115,9 @@ angular.module('timer', [])
$scope.addCDSeconds = $element[0].addCDSeconds = function(extraSeconds){
$scope.countdown += extraSeconds;
$scope.$digest();
if (!$scope.isRunning) {
$scope.start();
}
};

$scope.$on('timer-add-cd-seconds', function (e, extraSeconds) {
Expand Down Expand Up @@ -143,13 +152,6 @@ angular.module('timer', [])
return;
}
calculateTimeUnits();
if ($scope.countdown > 0) {
$scope.countdown--;
}
else if ($scope.countdown <= 0) {
$scope.stop();
return;
}

//We are not using $timeout for a reason. Please read here - https://github.com/siddii/angular-timer/pull/5
$scope.timeoutId = setTimeout(function () {
Expand All @@ -158,6 +160,13 @@ angular.module('timer', [])
}, $scope.interval - adjustment);

$scope.$emit('timer-tick', {timeoutId: $scope.timeoutId, millis: $scope.millis});

if ($scope.countdown > 0) {
$scope.countdown--;
}
else if ($scope.countdown <= 0) {
$scope.stop();
}
};

if ($scope.autoStart === undefined || $scope.autoStart === true) {
Expand Down
4 changes: 2 additions & 2 deletions dist/angular-timer.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/angularjs-add-countdown-seconds.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<body ng-app="MyApp">
<div ng-controller="MyAppController">
<h1>AngularJS - Add Countdown Seconds Example</h1>
<h3><timer countdown="100" interval="1000">{{countdown}}</timer></h3>
<h3><timer countdown="10" interval="1000">{{countdown}}</timer></h3>
<button ng-click="add5Seconds()">Add 5 Seconds</button>
</div>
<br/>
Expand Down

0 comments on commit a4fbd4d

Please sign in to comment.