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.7 to 0.0.8

128

accurate-timer.js

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

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