Skip to content

Instantly share code, notes, and snippets.

@IndigoW0lf
Created August 9, 2022 22:43
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 IndigoW0lf/157bce0c94d609e11c42369cf414edfc to your computer and use it in GitHub Desktop.
Save IndigoW0lf/157bce0c94d609e11c42369cf414edfc to your computer and use it in GitHub Desktop.
timer script using 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