Skip to content

Instantly share code, notes, and snippets.

@brachycera
Last active October 2, 2015 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brachycera/d1fd835e2142fc7b6267 to your computer and use it in GitHub Desktop.
Save brachycera/d1fd835e2142fc7b6267 to your computer and use it in GitHub Desktop.
/**
*
* 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