Skip to content

Commit

Permalink
Merge pull request #2 from BinaryMuse/mkt-allow-setting-timer-rate
Browse files Browse the repository at this point in the history
Add API for setting timer rate
  • Loading branch information
BinaryMuse committed Mar 10, 2016
2 parents c3889e9 + fe18900 commit 323817d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ Pauses the timer.

Sets the timer's current time (in milliseconds).

**`Timex#getRate()`**

Returns the timer's current rate. Defaults to `1 `.

**`Timex#setRate(rate)`**

Sets the timer's rate. The rate affects how the time is calculated, not how often the timer ticks. For example, setting `rate` to `0.5` makes the timer increment time twice as slowly, but the ticks will occur just as often as before.

**`Timex#register(function(time){})`**

Registers a callback that gets called each time the timer ticks. The callback receives the current time of the timer as its only argument.
Expand Down
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function Timer() {
this.currentTime = 0;
this.lastTick = null;
this.callbacks = [];
this._rate = 1;

this._tick = this._tick.bind(this);
}
Expand All @@ -32,6 +33,14 @@ Timer.prototype.pause = function() {
this.running = false;
};

Timer.prototype.setRate = function(rate) {
this._rate = rate;
};

Timer.prototype.getRate = function() {
return this._rate;
};

Timer.prototype._startTimer = function() {
this._tick();
};
Expand All @@ -44,7 +53,7 @@ Timer.prototype._tick = function() {
} else {
var thisTick = now(),
delta = thisTick - this.lastTick;
this.currentTime = this.currentTime + delta;
this.currentTime = this.currentTime + delta * this._rate;
this.lastTick = thisTick;
}

Expand Down
12 changes: 12 additions & 0 deletions test/browser.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
var Timex = require("../");
var timer = new Timex();
var timer2 = new Timex();

var div1 = document.createElement("div");
var div2 = document.createElement("div");
var div3 = document.createElement("div");

document.documentElement.appendChild(div1);
document.documentElement.appendChild(div2);
document.documentElement.appendChild(div3);

var updateDoc1 = function(time) {
div1.textContent = "" + time;
Expand All @@ -15,11 +18,20 @@ var updateDoc2 = function(time) {
div2.textContent = "" + time;
};

var updateDoc3 = function(time) {
div3.textContent = "" + time;
};

timer.register(updateDoc1)
timer.register(updateDoc2)

timer2.register(updateDoc3)

setTimeout(function() {
timer.unregister(updateDoc2);
}, 2000);

timer2.setRate(0.5);

timer.start();
timer2.start();
10 changes: 10 additions & 0 deletions test/node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var Timex = require("../");
var timer = new Timex();
var timer2 = new Timex();

var log1 = function(time) {
console.log("Timer 1: " + time);
Expand All @@ -9,11 +10,20 @@ var log2 = function(time) {
console.log("Timer 2: " + time);
};

var log3 = function(time) {
console.log("Timer 3: " + time);
};

timer.register(log1);
timer.register(log2);

timer2.register(log3);

setTimeout(function() {
timer.unregister(log2);
}, 2000);

timer2.setRate(0.5);

timer.start();
timer2.start();

0 comments on commit 323817d

Please sign in to comment.