@smarterservices/smarterclock
Advanced tools
Comparing version 1.1.1 to 2.0.0
24
index.js
@@ -15,3 +15,3 @@ var smarterClock = function (config) { | ||
//tickrate for getting delta from ntp server | ||
this.tickRate = config.syncDelay || 300; | ||
this.tickRate = config.syncDelay || 10; | ||
// set the tickRate to milliseconds. | ||
@@ -34,2 +34,5 @@ this.tickRate = this.tickRate * 1000; | ||
this.currentServer = this.ntpServers[this.currentIndex]; | ||
} else { | ||
this.currentIndex = 0; | ||
this.currentServer = this.ntpServers[this.currentIndex]; | ||
} | ||
@@ -47,2 +50,5 @@ }; | ||
smarterClock.prototype.getTime = function () { | ||
return ((new Date()).getTime() + this.getDrift()); | ||
}; | ||
smarterClock.prototype.getDrift = function () { | ||
//get sum of this.delta array | ||
@@ -55,3 +61,3 @@ var sum = this.delta.reduce(function (a, b) { | ||
//return your time +/- the avg delta | ||
return ((new Date()).getTime() + avg); | ||
return avg; | ||
}; | ||
@@ -71,2 +77,8 @@ //this function is used for a one off sync(adds one delta value to the this.delta array) | ||
this.shiftServer(); | ||
if(this.delta.length === 0) { | ||
setTimeout(() => { | ||
this.getDelta() | ||
},1000) | ||
} | ||
} else { | ||
@@ -76,5 +88,11 @@ //get delta value and push into this.delta array | ||
var tempLocalTime = (new Date()).getTime(); | ||
if (this.delta.length === this.limit) { | ||
if(tempServerTime <= 0) { | ||
this.getDelta() | ||
return; | ||
} | ||
if (this.delta.length >= this.limit) { | ||
this.delta.shift(); | ||
} | ||
this.delta.push(tempServerTime - tempLocalTime); | ||
@@ -81,0 +99,0 @@ //if callback passed in return current delta time |
{ | ||
"name": "@smarterservices/smarterclock", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"description": "store of delta values between ntp time and local time to get an accurate sync time", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
7764
106