The Stopwatch is not working. I have tried to place console.logs everywhere to see if something is not being executed, and all I can find is the if statement in the start method is not working. Implemented an else with it to see if the statement had condition issues, but the else would also fail to activate. All I can predict is that there is an un-thrown error in the if statement. Perhaps it is something else, but I have been trying to fix it for days and cannot figure it out.
class StopWatch {
constructor() {
this.sec = 0
this.min = 0
this.hour = 0
this.startButton = document.getElementById("start")
this.stopButton = document.getElementById("stop")
this.resetButton = document.getElementById("reset")
this.time = document.getElementById("timer")
this.stopped = true
}
start() {
if (this.stopped === true) {
this.stopped = false
this.count
}
}
stop() {
if (this.stopped === false) {
this.stopped = true
}
}
reset() {
this.stopped = true
this.time.innerHTML = '00:00:00'
}
count() {
if (this.stopped === false) {
this.sec = parseInt(this.sec)
this.min = parseInt(this.min)
this.hour = parseInt(this.hour)
this.sec += 1
if (this.sec == 60) {
this.sec = 0
this.min = 1
}
if (this.min == 60) {
this.min = 0
this.hour = 1
}
if (this.sec <= 10) {
this.sec = `0${this.sec}`
}
if (this.min <= 10) {
this.min = `0${this.min}`
}
if (this.hour <= 10) {
this.hour = `0${this.hour}`
}
this.time.innerHTML = `${this.hour}:${this.min}:${this.sec}`
setTimeout(this.count, 1000)
}
}
}
const watch = new StopWatch()
watch.startButton.onclick = watch.start
watch.stopButton.onclick = watch.stop
watch.resetButton.onclick = watch.reset
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="game.js" defer></script>
<style></style>
</head>
<body>
<h1>STOPWATCH</h1>
<h2>Vanilla Javascript Stopwatch</h2>
<span id="timer">00:00:00</span>
<button id="start">Start</button>
<button id="stop">Stop</button>
<button id="reset">Reset</button>
</body>
</html>