Skip to content

Instantly share code, notes, and snippets.

@acinader
Created May 3, 2018 16:25
Show Gist options
  • Save acinader/2ef2ee194e2ad431e5c9c5e4cb754ca4 to your computer and use it in GitHub Desktop.
Save acinader/2ef2ee194e2ad431e5c9c5e4cb754ca4 to your computer and use it in GitHub Desktop.
Format seconds into minutes:seconds
<html>
<span id='time'></span>
<script>
var formatTime = function formatTime(element, time) {
var min = Math.floor(time / 60);
var sec = time % 60;
element.innerHTML = min + ':' + pad(sec);
};
var pad = function pad(number) {
return number < 10 ? '0' + number : number;
};
var time = 300;
var timeDisplay = document.getElementById('time');
formatTime(timeDisplay, time);
var timerId = setInterval(function() {
time = time - 1;
formatTime(timeDisplay, time);
if (time === 0) {
clearInterval(timerId);
// Done!
}
}, 1000);
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment