Skip to content

Added countdown finished callback #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 28, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/js/timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ angular.module('timer', [])
startTimeAttr: '=startTime',
endTimeAttr: '=endTime',
countdownattr: '=countdown',
autoStart: '&autoStart'
autoStart: '&autoStart',
finishCallback: '&finishCallback'
},
controller: ['$scope', '$element', '$attrs', '$timeout', function ($scope, $element, $attrs, $timeout) {

Expand Down Expand Up @@ -153,6 +154,7 @@ angular.module('timer', [])
$scope.stop();
$scope.millis = 0;
calculateTimeUnits();
if($scope.finishCallback) $scope.$eval($scope.finishCallback);
return;
}
calculateTimeUnits();
Expand All @@ -170,6 +172,7 @@ angular.module('timer', [])
}
else if ($scope.countdown <= 0) {
$scope.stop();
if($scope.finishCallback) $scope.$eval($scope.finishCallback);
}
};

Expand Down
7 changes: 4 additions & 3 deletions dist/angular-timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ angular.module('timer', [])
startTimeAttr: '=startTime',
endTimeAttr: '=endTime',
countdownattr: '=countdown',
autoStart: '&autoStart'
autoStart: '&autoStart',
finishCallback: '&finishCallback'
},
controller: ['$scope', '$element', '$attrs', '$timeout', function ($scope, $element, $attrs, $timeout) {

Expand Down Expand Up @@ -112,14 +113,12 @@ angular.module('timer', [])
$scope.days = Math.floor((($scope.millis / (3600000)) / 24));
$scope.daysS = $scope.days==1 ? '' : 's';


//add leading zero if number is smaller than 10
$scope.sseconds = $scope.seconds < 10 ? '0' + $scope.seconds : $scope.seconds;
$scope.mminutes = $scope.minutes < 10 ? '0' + $scope.minutes : $scope.minutes;
$scope.hhours = $scope.hours < 10 ? '0' + $scope.hours : $scope.hours;
$scope.ddays = $scope.days < 10 ? '0' + $scope.days : $scope.days;


}
//determine initial values of time units and add AddSeconds functionality
if ($scope.countdownattr) {
Expand Down Expand Up @@ -162,6 +161,7 @@ angular.module('timer', [])
$scope.stop();
$scope.millis = 0;
calculateTimeUnits();
if($scope.finishCallback) $scope.$eval($scope.finishCallback);
return;
}
calculateTimeUnits();
Expand All @@ -179,6 +179,7 @@ angular.module('timer', [])
}
else if ($scope.countdown <= 0) {
$scope.stop();
if($scope.finishCallback) $scope.$eval($scope.finishCallback);
}
};

Expand Down
2 changes: 1 addition & 1 deletion dist/angular-timer.min.js

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

9 changes: 9 additions & 0 deletions docs/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,13 @@ angular.module('timer-demo',['timer']).controller('TimerDemoController',['$scope
}
});
};

$scope.callbackTimer={};
$scope.callbackTimer.status='Running';
$scope.callbackTimer.callbackCount=0;
$scope.callbackTimer.finished=function(){
$scope.callbackTimer.status='COMPLETE!!';
$scope.callbackTimer.callbackCount++;
$scope.$apply();
};
}]);
20 changes: 20 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,26 @@ <h3 class="plural-counter">
</div>
</section>


<section id="finish-callback-timer">
<h3>Countdown Finished Callback</h3>

<div class="bs-docs-example">
<p>
A countdown timer that updates a value once the callback is reached
<code ng-non-bindable="">
&lt;timer countdown="3" interval="1000" finish-callback="callbackTimer.finished()"&gt;{{seconds}} second{{secondsS}}&lt;/timer&gt;
</code>

<h3 class="counter">
<timer countdown="3" interval="1000" finish-callback="callbackTimer.finished()">{{seconds}} second{{secondsS}} </timer>
</h3>
Timer: <span class="timer-status">{{callbackTimer.status}}</span>
Callbacks: <span class="timer-callbacks">{{callbackTimer.callbackCount}}</span>

</div>
</section>

<section id="markup">
<h3>
Markup</h3>
Expand Down
12 changes: 12 additions & 0 deletions test/e2e/scenarios.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,17 @@ describe('Angular Timer E2E Tests', function () {
expect(element('#clock-timer-leading-zero timer').html()).toMatch(/00 minutes,/);
expect(element('#clock-timer-leading-zero timer').html()).toMatch(/11 seconds./);
});

it('Countdown finish - Should fire callback on completion', function () {


expect(element('#finish-callback-timer .timer-status').html()).toBe('Running');
expect(element('#finish-callback-timer .timer-callbacks').html()).toBe('0');

sleep(5);
expect(element('#finish-callback-timer .timer-status').html()).toBe('COMPLETE!!');
expect(element('#finish-callback-timer .timer-callbacks').html()).toBe('1');

});

});