Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
*
* Simple Countdown Function
* @param {num} duration - in Milliseconds
* @param {string} elemID - DOM Object ID to show the Countdown
*
*/
function countdown(duration, elemID) {
var start = Date.now( );
var difference, hours, minutes, seconds;
var elem = document.getElementById( elemID );
function innerTimer( ) {
difference = duration - ( ( ( Date.now( ) - start ) / 1000 ) | 0 );
hours = ( difference / 3600 ) | 0;
minutes = ( (difference % 3600 ) / 60) | 0;
seconds = ( difference % 60 ) | 0;
hours = ( hours < 10 ) ? '0' + hours : hours;
hours = hours + ':';
minutes = minutes < 10 ? '0' + minutes : minutes;
minutes = minutes + ':';
seconds = seconds < 10 ? '0' + seconds : seconds;
elem.innerHTML = hours + minutes + seconds;
if ( difference <= 0 ) {
start = Date.now( ) + 1000;
}
}
innerTimer( );
setInterval( innerTimer, 1000 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment