Two Factor Timer
var updateTimer = function(period, updateTokenCallback) { | |
// Get current time seconds | |
var epocSecs = Math.floor((+new Date()) / 1000); | |
var sec = epocSecs % period; | |
var secsToNext = 0; | |
// Fire if clock has struck 0 or 'period' | |
if (sec == 0 || sec == period) { | |
updateTokenCallback(); | |
} else { | |
// If less than 'period', next update is at 'period' | |
if (sec < period) { | |
secsToNext = period - sec; | |
} else if (sec > period) { | |
secsToNext = period - sec; | |
} | |
} | |
return secsToNext; | |
}; | |
var myTimer = function() { | |
var secsToNext = updateTimer(30, function() { console.log("Updating token...") }); | |
console.log("Your token expires in: " + secsToNext); | |
}; | |
setInterval(myTimer, 500); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment