Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

accurate-timer

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

accurate-timer - npm Package Compare versions

Comparing version 0.0.8 to 0.0.9

128

accurate-timer.js

@@ -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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc