Looking for advice on simplifying my JS stopwatch timer that currently only activates once and keeps running indefinitely. As a newcomer to JS, this is the best solution I could come up with:
let time = 0
let activated = 0
function changePic() {
if(activated == 0) {
interval()
activated++
}
}
function interval() {
setInterval(timer, 1000)
}
function timer() {
time++
document.getElementById("timer").innerHTML = time + " seconds"
}
I have omitted unnecessary code.
Now, I am curious about two things:
- How can I simplify this code further?
- What steps do I need to take to add a stop button to halt the timer?
It's important to note that the timer should only activate once until stopped, at which point it should reset to 0 upon activation again.