accurate-timer
Advanced tools
Comparing version 0.0.7 to 0.0.8
@@ -1,80 +0,76 @@ | ||
(function(root){ | ||
var Timer = function(callback, interval){ | ||
this.raf = window.requestAnimationFrame | ||
|| window.mozRequestAnimationFrame | ||
|| window.webkitRequestAnimationFrame | ||
|| window.msRequestAnimationFrame; | ||
var Timer = function(callback, interval){ | ||
this.raf = window.requestAnimationFrame | ||
|| window.mozRequestAnimationFrame | ||
|| window.webkitRequestAnimationFrame | ||
|| window.msRequestAnimationFrame; | ||
if (this.raf) { | ||
this.type = 'requestAnimationFrame'; | ||
} else { | ||
this.type = 'setInterval'; | ||
} | ||
if (this.raf) { | ||
this.type = 'requestAnimationFrame'; | ||
} else { | ||
this.type = 'setInterval'; | ||
} | ||
this.timer = null; | ||
this.callback = callback; | ||
this.interval = interval; | ||
} | ||
this.timer = null; | ||
this.callback = callback; | ||
this.interval = interval; | ||
} | ||
Timer.prototype.start = function(){ | ||
this.stop(); | ||
var that = this; | ||
Timer.prototype.start = function(){ | ||
this.stop(); | ||
var that = this; | ||
switch (this.type) { | ||
case 'requestAnimationFrame': | ||
console.log('using raf') | ||
that._startRAF(); | ||
break; | ||
case 'setInterval': | ||
console.log('using SI') | ||
that._startSI(); | ||
break; | ||
} | ||
} | ||
switch (this.type) { | ||
case 'requestAnimationFrame': | ||
console.log('using raf') | ||
that._startRAF(); | ||
break; | ||
case 'setInterval': | ||
console.log('using SI') | ||
that._startSI(); | ||
break; | ||
} | ||
} | ||
Timer.prototype.stop = function(){ | ||
if (this.animationFrame) { | ||
window.cancelAnimationFrame(this.animationFrame); | ||
delete this.animationFrame; | ||
} else if (this.intervalTimer) { | ||
clearInterval(this.intervalTimer); | ||
delete this.intervalTimer; | ||
} | ||
} | ||
Timer.prototype.stop = function(){ | ||
if (this.animationFrame) { | ||
window.cancelAnimationFrame(this.animationFrame); | ||
delete this.animationFrame; | ||
} else if (this.intervalTimer) { | ||
clearInterval(this.intervalTimer); | ||
delete this.intervalTimer; | ||
} | ||
} | ||
Timer.prototype._startRAF = function(){ | ||
if (!this.raf) throw(new Error('Trying to use non-existent requestAnimationFrame')); | ||
Timer.prototype._startRAF = function(){ | ||
if (!this.raf) throw(new Error('Trying to use non-existent requestAnimationFrame')); | ||
this._lastTimestamp = performance.now() | ||
this.raf.call(window, this._stepRAF.bind(this)) | ||
} | ||
this._lastTimestamp = performance.now() | ||
this.raf.call(window, this._stepRAF.bind(this)) | ||
} | ||
Timer.prototype._stepRAF = function(timestamp){ | ||
var elapsedTime = performance.now() - this._lastTimestamp | ||
Timer.prototype._stepRAF = function(timestamp){ | ||
var elapsedTime = performance.now() - this._lastTimestamp | ||
if (elapsedTime >= this.interval){ | ||
this._lastTimestamp = this._lastTimestamp + this.interval; | ||
this.callback(performance.now()); | ||
} | ||
if (elapsedTime >= this.interval){ | ||
this._lastTimestamp = this._lastTimestamp + this.interval; | ||
this.callback(performance.now()); | ||
} | ||
this.animationFrame = this.raf.call(window, this._stepRAF.bind(this)); | ||
} | ||
this.animationFrame = this.raf.call(window, this._stepRAF.bind(this)); | ||
} | ||
Timer.prototype._startSI = function(){ | ||
this._lastTimestamp = (new Date).getTime() | ||
var that = this; | ||
Timer.prototype._startSI = function(){ | ||
this._lastTimestamp = (new Date).getTime() | ||
var that = this; | ||
this.intervalTimer = setInterval(function(){ | ||
var elapsedTime = (new Date).getTime() - that._lastTimestamp; | ||
this.intervalTimer = setInterval(function(){ | ||
var elapsedTime = (new Date).getTime() - that._lastTimestamp; | ||
if (elapsedTime >= that.interval) { | ||
that._lastTimestamp = that._lastTimestamp + that.interval; | ||
that.callback((new Date).getTime()); | ||
} | ||
}, 1) | ||
} | ||
if (elapsedTime >= that.interval) { | ||
that._lastTimestamp = that._lastTimestamp + that.interval; | ||
that.callback((new Date).getTime()); | ||
} | ||
}, 1) | ||
} | ||
return Timer | ||
})(this); | ||
module.exports = Timer |
{ | ||
"name": "accurate-timer", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"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
5458
62