Skip to content

Instantly share code, notes, and snippets.

@marcellocurto
Created April 22, 2020 12:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcellocurto/3170b520b91fe95bb0580fd032e4f3bf to your computer and use it in GitHub Desktop.
Save marcellocurto/3170b520b91fe95bb0580fd032e4f3bf to your computer and use it in GitHub Desktop.
A stopwatch in Javascript
// 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