Last active
July 24, 2016 04:23
-
-
Save encody/a9b7a430efeb3c37b6b41315707b6d46 to your computer and use it in GitHub Desktop.
Stopwatch
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
| <!-- | |
| Video Guide: https://youtu.be/fF-vtP3sjPc | |
| --> | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>Stopwatch</title> | |
| <link rel="stylesheet" href="stopwatch.css"> | |
| </head> | |
| <body> | |
| <div class="stopwatch"> | |
| <div class="controls"> | |
| <button class="start">Start</button> | |
| <button class="stop">Stop</button> | |
| <button class="reset">Reset</button> | |
| </div> | |
| <div class="display"> | |
| <span class="minutes">00</span><span class="seconds">00</span><span class="centiseconds">00</span> | |
| </div> | |
| </div> | |
| <script src="stopwatch.js"></script> | |
| </body> | |
| </html> |
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
| /* | |
| Video Guide: https://youtu.be/fF-vtP3sjPc | |
| */ | |
| var ss = document.getElementsByClassName('stopwatch'); | |
| [].forEach.call(ss, function (s) { | |
| var currentTimer = 0, | |
| interval = 0, | |
| lastUpdateTime = new Date().getTime(), | |
| start = s.querySelector('button.start'), | |
| stop = s.querySelector('button.stop'), | |
| reset = s.querySelector('button.reset'), | |
| mins = s.querySelector('span.minutes'), | |
| secs = s.querySelector('span.seconds'), | |
| cents = s.querySelector('span.centiseconds'); | |
| start.addEventListener('click', startTimer); | |
| stop.addEventListener('click', stopTimer); | |
| reset.addEventListener('click', resetTimer); | |
| function pad (n) { | |
| return ('00' + n).substr(-2); | |
| } | |
| function update () { | |
| var now = new Date().getTime(), | |
| dt = now - lastUpdateTime; | |
| currentTimer += dt; | |
| var time = new Date(currentTimer); | |
| mins.innerHTML = pad(time.getMinutes()); | |
| secs.innerHTML = pad(time.getSeconds()); | |
| cents.innerHTML = pad(Math.floor(time.getMilliseconds() / 10)); | |
| lastUpdateTime = now; | |
| } | |
| function startTimer () { | |
| if (!interval) { | |
| lastUpdateTime = new Date().getTime(); | |
| interval = setInterval(update, 1); | |
| } | |
| } | |
| function stopTimer () { | |
| clearInterval(interval); | |
| interval = 0; | |
| } | |
| function resetTimer () { | |
| stopTimer(); | |
| currentTimer = 0; | |
| mins.innerHTML = secs.innerHTML = cents.innerHTML = pad(0); | |
| } | |
| }); |
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
| /* | |
| Video Guide: https://youtu.be/fF-vtP3sjPc | |
| */ | |
| .stopwatch { | |
| width: 300px; | |
| background-color: #0af; | |
| border-radius: 5px; | |
| box-shadow: 0 4px rgba(0, 0, 0, 0.75), 0 0 1px rgba(0, 0, 0, 0.15); | |
| padding: 15px; | |
| &, & * { | |
| transition: all 0.15s ease-out; | |
| } | |
| .controls { | |
| display: flex; | |
| button { | |
| flex-grow: 1; | |
| margin: 0 5px 4px; | |
| padding: 5px 0; | |
| border-radius: 5px; | |
| box-shadow: 0 4px rgba(0, 0, 0, 0.75); | |
| border: 0; | |
| outline: 0; | |
| font-size: 16px; | |
| color: white; | |
| cursor: pointer; | |
| font-weight: bold; | |
| &:active { | |
| margin-bottom: 0; | |
| margin-top: 4px; | |
| box-shadow: none; | |
| } | |
| } | |
| .start { | |
| background-color: #5d5; | |
| &:hover { | |
| background-color: #6e6; | |
| } | |
| } | |
| .stop { | |
| background-color: #d55; | |
| &:hover { | |
| background-color: #e66; | |
| } | |
| } | |
| .reset { | |
| background-color: #55d; | |
| &:hover { | |
| background-color: #66e; | |
| } | |
| } | |
| } | |
| .display { | |
| font-size: 50px; | |
| font-family: sans-serif; | |
| text-align: center; | |
| margin-top: 10px; | |
| :not(:last-child):after { | |
| content: ':'; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment