I am a beginner trying to learn JavaScript. My project is to make a stopwatch with stop, reset, and go.
For some reason, I can't figure out how to get clearInterval() to stop the goFunction. Any help would be appreciated.
var min = 00
var sec = 00
var ms = 00
function goFunction() {
var swTimer = setInterval(addTime, 10)
function addTime() {
if (ms < 99) {
ms++
} else {
sec += 1
ms = 0
}
if (sec > 59) {
min += 1
sec = 0
}
msShow = (ms < 10) ? "0" + ms : ms
secShow = (sec < 10) ? "0" + sec : sec
minShow = (min < 10) ? "0" + min : min
document.getElementById("numbers").innerHTML = minShow + ":" + secShow + ":" + msShow
}
}
function stopFunction() {
document.getElementById("numbers").style.color = "red"
clearInterval(swTimer);
}
<div class="controls">
<button onclick="goFunction()" class="button button1">GO</button>
<button onclick="resetFunction()" class="button button2">RESET</button>
<button onclick="stopFunction()" class="button button3">STOP</button>
<div class="display" id="numbers">00:00:00</div>
</div>