Skip to content

Instantly share code, notes, and snippets.

@dineshsprabu
Created October 20, 2016 15:09
Show Gist options
  • Save dineshsprabu/b4117ab6de2f205e923c2c16ebe5d739 to your computer and use it in GitHub Desktop.
Save dineshsprabu/b4117ab6de2f205e923c2c16ebe5d739 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