Last active
December 25, 2015 18:59
-
-
Save yakirh/7024262 to your computer and use it in GitHub Desktop.
JS Timer exercise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!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