setInterval() Inconsistencies
I am trying to create an object that works like a stopwatch, but there is a 100 millisecond difference between Chrome and Firefox browsers. The code and test below fails in both Safari and Chrome but passes in Firefox.
Question: Why does this happen?
Questions: How to achieve Consistency with this object across browsers?
You can run this SAMPLE in several browsers and watch the test pass or fail. The sample is on JSBIN so you can also make code changes to it.
Timer Object (SUT)
I've built a Timer object that essentially works like a stopwatch:
function Timer() {
this.interval = null;
this.time = 0;
}
Timer.prototype.start = function () {
var self = this;
this.interval = setInterval(function () {
self.time += 100;
}, 100);
};
Timer.prototype.stop = function () {
clearInterval(this.interval);
};
Timer Test (jasmine)
This test starts and stops the timer. It fails in Chrome but passes in FireFox.
describe("Timer", function () {
it("should stop and record time accurately", function (done) {
var timer = new Timer();
timer.start();
setTimeout(function () {
expect(timer.time).toEqual(600);
done();
}, 600);
});
});