Skip to content

Instantly share code, notes, and snippets.

@codenamezjames
Created June 16, 2016 18:30
Show Gist options
  • Select an option

  • Save codenamezjames/252cbbd9b64444c01f65428b2b65163d to your computer and use it in GitHub Desktop.

Select an option

Save codenamezjames/252cbbd9b64444c01f65428b2b65163d to your computer and use it in GitHub Desktop.
function getDiff(beginningTime) {
var now = Math.floor(Date.now() / 1000); // get the time now
var diff = Math.abs(now - beginningTime); // diff in seconds between now and start
return diff;
}
function getTimmer(diff, format){
var h = Math.floor(diff / 3600); // diff devided by seconds in an hour
var m = Math.floor(diff / 60) - (h*60); // get minutes value (quotient of diff minus hours)
var s = Math.floor(diff % 60); // get seconds value (remainder of diff)
return { seconds: s, minutes: m, hours: h };
}
var runClock = (function (){
var lastDiff = 0;
return function tick(done, update, countTime, endTime) {
var diff = getDiff(countTime);
var timeElapsed = getTimmer(diff);
if(diff === 0 && lastDiff !== 0){
return typeof done === 'function' ? done(timeElapsed, diff) : '';
}
if(diff !== lastDiff){
lastDiff = diff;
return typeof update === 'function' ? update(timeElapsed, diff) : '';
}
}
})();
var countDown = Math.floor(Date.now() / 1000)
runClock(null, function(e, r){ console.log( e.seconds );}, countDown);
var t = setInterval(function(){
runClock(function(){
console.log('done');
clearInterval(t);
},function(timeElapsed, timeRemaining){
console.log( timeElapsed.seconds );
}, countDown);
}, 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment