Skip to content
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

Allow user to specify the step interval #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 15 additions & 6 deletions lib/countdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,31 @@

var root = this;

var Countdown = function(duration, onTick, onComplete) {
var Countdown = function(duration, onTick, onComplete, step) {
var secondsLeft = Math.round(duration)
, step = (step > 0) ? Math.round(step) : 1
, tick = function() {
if (secondsLeft > 0) {
onTick(secondsLeft);
secondsLeft -= 1;
onTick(secondsLeft);
if (secondsLeft >= step) {
secondsLeft -= step;
}
else {
// Clear the current interval, and start it up with the the remaining seconds left.
clearInterval(interval);
interval = setInterval(
function() { tick.call(this) }, 1000 * secondsLeft, this
);
secondsLeft = 0;
}
} else {
clearInterval(interval);
onComplete();
}
}
// Setting the interval, by call tick and passing through this via a self-calling function wrap.
, interval = setInterval(
(function(self){
return function() { tick.call(self); };
})(this), 1000
function() { tick.call(this) }, 1000 * step, this
);

// First tick.
Expand Down
43 changes: 43 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,49 @@ describe("countdown.js", function() {
assert(tickFunction.callCount == 5);
});

it("executes onTick while countdown is running with user specified step interval", function() {
new Countdown(5, tickFunction, callbackFunction, 2);
assert(tickFunction.callCount == 1);
clock.tick(1000);
assert(tickFunction.callCount == 1);
clock.tick(1000);
assert(tickFunction.callCount == 2);
clock.tick(1000);
assert(tickFunction.callCount == 2);
clock.tick(1000);
assert(tickFunction.callCount == 3);
clock.tick(1000);
assert(tickFunction.callCount == 3);
});

it("defaults to 1 second ticks with invalid step parameter", function() {
new Countdown(2, tickFunction, callbackFunction, -1);
assert(tickFunction.callCount == 1);
clock.tick(1000);
assert(tickFunction.callCount == 2);
clock.tick(1000);
new Countdown(2, tickFunction, callbackFunction, []);
assert(tickFunction.callCount == 3);
clock.tick(1000);
assert(tickFunction.callCount == 4);
clock.tick(1000);
new Countdown(2, tickFunction, callbackFunction, {});
assert(tickFunction.callCount == 5);
clock.tick(1000);
assert(tickFunction.callCount == 6);
clock.tick(1000);
new Countdown(2, tickFunction, callbackFunction, 'invalid');
assert(tickFunction.callCount == 7);
clock.tick(1000);
assert(tickFunction.callCount == 8);
clock.tick(1000);
new Countdown(2, tickFunction, callbackFunction, true);
assert(tickFunction.callCount == 9);
clock.tick(1000);
assert(tickFunction.callCount == 10);
clock.tick(1000);
});

it("executes onComplete", function() {
new Countdown(5, tickFunction, callbackFunction);
clock.tick(4000);
Expand Down