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

Add support for floating point seconds as a duration #7

Closed
wants to merge 1 commit into from
Closed
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
30 changes: 23 additions & 7 deletions lib/countdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,45 @@
var root = this;

var Countdown = function(duration, onTick, onComplete) {
var secondsLeft = duration
var secondsLeft = parseFloat(duration)
, tick = function() {
if (secondsLeft > 0) {
onTick(secondsLeft);
secondsLeft -= 1;

if (secondsLeft >= 1.0) {
secondsLeft -= 1.0;
} else {
clearInterval(interval);

// Assign a new timeout here so we can abort still if < 1 second
setTimeout(function() {
onComplete();
}, secondsLeft * 1000);
}
} else {
clearInterval(interval);
onComplete();
clearInterval(interval);
onComplete();
}
}
// Setting the interval, by call tick and passing through this via a self-calling function wrap.
// Setting the interval, by calling tick() and passing through this via a self-calling function wrap.
, interval = setInterval(
(function(self){
return function() { tick.call(self); };
})(this), 1000
);
)
, timeout;

// First tick.
tick.call(this);

return {
abort: function() {
clearInterval(interval);
// We set the timeout and are definitely < 1 second here
if (typeof timeout !== "undefined") {
clearTimeout(timeout);
} else {
clearInterval(interval);
}
}

, getRemainingTime: function() {
Expand Down
26 changes: 25 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,30 @@ describe("countdown.js", function() {
assert(tickFunction.callCount == 5);
});

it("executes onComplete", function() {
it("executes onTick while countdown is running for floats on integer boundaries", function() {
new Countdown(1.5, tickFunction, callbackFunction);
assert(tickFunction.callCount == 1);
clock.tick(1000);
assert(tickFunction.callCount == 2);
clock.tick(1000);
assert(tickFunction.callCount == 2);
});

it("executes onComplete for integers", function() {
new Countdown(5, tickFunction, callbackFunction);
clock.tick(4000);
assert(callbackFunction.callCount == 0);
clock.tick(5000);
assert(callbackFunction.callCount == 1);
});

it("executes onComplete on for floats", function() {
new Countdown(1.5, tickFunction, callbackFunction);
clock.tick(1000);
assert(callbackFunction.callCount == 0);
clock.tick(1000);
assert(callbackFunction.callCount == 1);
});
});

describe("aborting countdown", function() {
Expand All @@ -63,6 +80,13 @@ describe("countdown.js", function() {
clock.tick(5000);
assert(callbackFunction.callCount == 0);
});

it("does not call onComplete when aborted with < 1 second", function() {
clock.tick(4000);
countdown.abort();
clock.tick(1000);
assert(callbackFunction.callCount == 0);
});
});

describe("getRemainingTime", function() {
Expand Down