Created
April 22, 2020 12:00
-
-
Save marcellocurto/3170b520b91fe95bb0580fd032e4f3bf to your computer and use it in GitHub Desktop.
A stopwatch in Javascript
This file contains 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
// Set starting variables for stopwatch | |
let minutes = 0 | |
let seconds = 0 | |
let secondsAsString = '00' | |
let minutesAsString = '00' | |
// Starts the stopwatch | |
let startTimer = () => setInterval(timerLogic, 1000) | |
// Logic behind the stopwatch | |
let timerLogic = () => { | |
if (seconds === 59) { minutes++; seconds = 0 } else { seconds++ } | |
seconds < 10 ? secondsAsString = '0' + seconds.toString() : secondsAsString = seconds.toString() | |
minutes < 10 ? minutesAsString = '0' + minutes.toString() : minutesAsString = minutes.toString() | |
timerCounter.textContent = minutesAsString + ":" + secondsAsString | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment