accurate-timer
Advanced tools
Comparing version 0.0.8 to 0.0.9
@@ -1,76 +0,80 @@ | ||
var Timer = function(callback, interval){ | ||
this.raf = window.requestAnimationFrame | ||
|| window.mozRequestAnimationFrame | ||
|| window.webkitRequestAnimationFrame | ||
|| window.msRequestAnimationFrame; | ||
(function(root){ | ||
if (this.raf) { | ||
this.type = 'requestAnimationFrame'; | ||
} else { | ||
this.type = 'setInterval'; | ||
} | ||
var Timer = function(callback, interval){ | ||
this.raf = window.requestAnimationFrame | ||
|| window.mozRequestAnimationFrame | ||
|| window.webkitRequestAnimationFrame | ||
|| window.msRequestAnimationFrame; | ||
this.timer = null; | ||
this.callback = callback; | ||
this.interval = interval; | ||
} | ||
if (this.raf) { | ||
this.type = 'requestAnimationFrame'; | ||
} else { | ||
this.type = 'setInterval'; | ||
} | ||
Timer.prototype.start = function(){ | ||
this.stop(); | ||
var that = this; | ||
this.timer = null; | ||
this.callback = callback; | ||
this.interval = interval; | ||
} | ||
switch (this.type) { | ||
case 'requestAnimationFrame': | ||
console.log('using raf') | ||
that._startRAF(); | ||
break; | ||
case 'setInterval': | ||
console.log('using SI') | ||
that._startSI(); | ||
break; | ||
} | ||
} | ||
Timer.prototype.start = function(){ | ||
this.stop(); | ||
var that = this; | ||
Timer.prototype.stop = function(){ | ||
if (this.animationFrame) { | ||
window.cancelAnimationFrame(this.animationFrame); | ||
delete this.animationFrame; | ||
} else if (this.intervalTimer) { | ||
clearInterval(this.intervalTimer); | ||
delete this.intervalTimer; | ||
} | ||
} | ||
switch (this.type) { | ||
case 'requestAnimationFrame': | ||
console.log('using raf') | ||
that._startRAF(); | ||
break; | ||
case 'setInterval': | ||
console.log('using SI') | ||
that._startSI(); | ||
break; | ||
} | ||
} | ||
Timer.prototype._startRAF = function(){ | ||
if (!this.raf) throw(new Error('Trying to use non-existent requestAnimationFrame')); | ||
Timer.prototype.stop = function(){ | ||
if (this.animationFrame) { | ||
window.cancelAnimationFrame(this.animationFrame); | ||
delete this.animationFrame; | ||
} else if (this.intervalTimer) { | ||
clearInterval(this.intervalTimer); | ||
delete this.intervalTimer; | ||
} | ||
} | ||
this._lastTimestamp = performance.now() | ||
this.raf.call(window, this._stepRAF.bind(this)) | ||
} | ||
Timer.prototype._startRAF = function(){ | ||
if (!this.raf) throw(new Error('Trying to use non-existent requestAnimationFrame')); | ||
Timer.prototype._stepRAF = function(timestamp){ | ||
var elapsedTime = performance.now() - this._lastTimestamp | ||
this._lastTimestamp = performance.now() | ||
this.raf.call(window, this._stepRAF.bind(this)) | ||
} | ||
if (elapsedTime >= this.interval){ | ||
this._lastTimestamp = this._lastTimestamp + this.interval; | ||
this.callback(performance.now()); | ||
} | ||
Timer.prototype._stepRAF = function(timestamp){ | ||
var elapsedTime = performance.now() - this._lastTimestamp | ||
this.animationFrame = this.raf.call(window, this._stepRAF.bind(this)); | ||
} | ||
if (elapsedTime >= this.interval){ | ||
this._lastTimestamp = this._lastTimestamp + this.interval; | ||
this.callback(performance.now()); | ||
} | ||
Timer.prototype._startSI = function(){ | ||
this._lastTimestamp = (new Date).getTime() | ||
var that = this; | ||
this.animationFrame = this.raf.call(window, this._stepRAF.bind(this)); | ||
} | ||
this.intervalTimer = setInterval(function(){ | ||
var elapsedTime = (new Date).getTime() - that._lastTimestamp; | ||
Timer.prototype._startSI = function(){ | ||
this._lastTimestamp = (new Date).getTime() | ||
var that = this; | ||
if (elapsedTime >= that.interval) { | ||
that._lastTimestamp = that._lastTimestamp + that.interval; | ||
that.callback((new Date).getTime()); | ||
} | ||
}, 1) | ||
} | ||
this.intervalTimer = setInterval(function(){ | ||
var elapsedTime = (new Date).getTime() - that._lastTimestamp; | ||
module.exports = Timer | ||
if (elapsedTime >= that.interval) { | ||
that._lastTimestamp = that._lastTimestamp + that.interval; | ||
that.callback((new Date).getTime()); | ||
} | ||
}, 1) | ||
} | ||
return Timer | ||
})(this); |
{ | ||
"name": "accurate-timer", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "An accurate timer using requestAnimationFrame() and performance.now(), defaulting to setInterval when needed.", | ||
@@ -5,0 +5,0 @@ "main": "accurate-timer.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
63
5414