Last active
October 2, 2015 22:57
-
-
Save brachycera/d1fd835e2142fc7b6267 to your computer and use it in GitHub Desktop.
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
/** | |
* | |
* 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