Last active
March 20, 2016 22:43
-
-
Save rtfpessoa/3a4d4047028d3cf22b2f to your computer and use it in GitHub Desktop.
Two Factor Timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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