-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #10 - add d3.{timeout,interval}.
- Loading branch information
Showing
8 changed files
with
129 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,13 @@ | ||
export { | ||
now, | ||
timer, | ||
timerOnce, | ||
timerFlush | ||
} from "./src/timer"; | ||
|
||
export { | ||
default as timeout | ||
} from "./src/timeout"; | ||
|
||
export { | ||
default as interval | ||
} from "./src/interval"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {Timer, now} from "./timer"; | ||
|
||
export default function(callback, delay, time) { | ||
var t = new Timer, d = delay; | ||
if (delay == null) return t.restart(callback, delay, time), t; | ||
delay = +delay, time = time == null ? now() : +time; | ||
t.restart(function tick(elapsed) { | ||
t.restart(tick, d += delay, time); | ||
callback(elapsed - delay + d); | ||
}, d, time); | ||
return t; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {Timer} from "./timer"; | ||
|
||
export default function(callback, delay, time) { | ||
var t = new Timer; | ||
delay = delay == null ? 0 : +delay; | ||
t.restart(function(elapsed) { | ||
t.stop(); | ||
callback(elapsed + delay); | ||
}, delay, time); | ||
return t; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
var tape = require("tape"), | ||
timer = require("../"), | ||
end = require("./end"); | ||
|
||
require("./inRange"); | ||
|
||
// It’s difficult to test the timing behavior reliably, since there can be small | ||
// hiccups that cause a timer to be delayed. So we test only the mean rate. | ||
tape("interval(callback) invokes the callback about every 17ms", function(test) { | ||
var then = timer.now(), count = 0; | ||
var t = timer.interval(function() { | ||
if (++count > 10) { | ||
t.stop(); | ||
test.inRange(timer.now() - then, (17 - 5) * count, (17 + 5) * count); | ||
end(test); | ||
} | ||
}); | ||
}); | ||
|
||
tape("interval(callback) invokes the callback until the timer is stopped", function(test) { | ||
var count = 0; | ||
var t = timer.interval(function() { | ||
if (++count > 2) { | ||
t.stop(); | ||
end(test); | ||
} | ||
}); | ||
}); | ||
|
||
tape("interval(callback, delay) invokes the callback about every delay milliseconds", function(test) { | ||
var then = timer.now(), delay = 50, count = 0; | ||
var t = timer.interval(function() { | ||
if (++count > 10) { | ||
t.stop(); | ||
test.inRange(timer.now() - then, (delay - 5) * count, (delay + 5) * count); | ||
end(test); | ||
} | ||
}, delay); | ||
}); | ||
|
||
tape("interval(callback, delay, time) invokes the callback repeatedly after the specified delay relative to the given time", function(test) { | ||
var then = timer.now() + 50, delay = 50; | ||
var t = timer.interval(function(elapsed) { | ||
test.inRange(timer.now() - then, delay - 10, delay + 10); | ||
t.stop(); | ||
end(test); | ||
}, delay, then); | ||
}); | ||
|
||
tape("interval(callback) uses the global context for the callback", function(test) { | ||
var t = timer.interval(function() { | ||
test.equal(this, global); | ||
t.stop(); | ||
end(test); | ||
}); | ||
}); | ||
|
||
tape("interval(callback) passes the callback the elapsed time", function(test) { | ||
var then = timer.now(), count = 0; | ||
var t = timer.interval(function(elapsed) { | ||
test.equal(elapsed, timer.now() - then); | ||
t.stop(); | ||
end(test); | ||
}, 100); | ||
}); | ||
|
||
tape("interval(callback) returns a timer", function(test) { | ||
var count = 0; | ||
var t = timer.interval(function() { ++count; }); | ||
test.equal(t instanceof timer.timer, true); | ||
t.stop(); | ||
setTimeout(function() { | ||
test.equal(count, 0); | ||
end(test); | ||
}, 100); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters