Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save asantos071/6fdd7e5a34b9f68ba41f559e9eab19f4 to your computer and use it in GitHub Desktop.
Save asantos071/6fdd7e5a34b9f68ba41f559e9eab19f4 to your computer and use it in GitHub Desktop.
[JAVASCRIPT] Timer with javascript.
<script>
function pretty_time_string(num) {
return ( num < 10 ? "0" : "" ) + num;
}
var start = new Date;
setInterval(function() {
var total_seconds = (new Date - start) / 1000;
var hours = Math.floor(total_seconds / 3600);
total_seconds = total_seconds % 3600;
var minutes = Math.floor(total_seconds / 60);
total_seconds = total_seconds % 60;
var seconds = Math.floor(total_seconds);
hours = pretty_time_string(hours);
minutes = pretty_time_string(minutes);
seconds = pretty_time_string(seconds);
var currentTimeString = hours + ":" + minutes + ":" + seconds;
$('.timer').text(currentTimeString); //jQuery needed
}, 1000);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment