Skip to content

Instantly share code, notes, and snippets.

@yakirh
Last active December 25, 2015 18:59
Show Gist options
  • Select an option

  • Save yakirh/7024262 to your computer and use it in GitHub Desktop.

Select an option

Save yakirh/7024262 to your computer and use it in GitHub Desktop.
JS Timer exercise
<!DOCTYPE html>
<html>
<head>
<title>Timer</title>
</head>
<body>
<h2>Current Time: </h2>
<div id="time"></div>
<script type="text/javascript">
var now,
timeEl = document.getElementById('time');
/**
* Make sure the number is in 2 digit format
* - Good: 09
* - Bad: 9
*
* @param {int} number
* @returns {string}
*/
function prependZero(number) {
return ('0' + number).slice(-2);
}
// Display current time in format HH:MM:SS
function displayTime() {
now = new Date;
timeEl.innerHTML =
prependZero(now.getHours())
+':'+
prependZero(now.getMinutes())
+':'+
prependZero(now.getSeconds());
}
displayTime();
setInterval(displayTime, 1000);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment