This is the snippet of code that is causing issues, basically the cool-down is not being respected. The web worker is just an interval that sends a message every 1 second. The idea behind this is that it displays how long in milliseconds it took someone to press space when the word dot is displayed in the canvas. It seems to work, accept the cool-down is being ignored and the new text is being displayed before the cool-down is over. I feel like it's obvious but I'm having no luck solving it.
const TheWorker = new Worker("worker.js")
let CurrentCount = 0;
let Waiting = false
let Cooldown = false
let SentTime = 0
TheWorker.addEventListener("message", async function (Message) {
if (Message.data == "SecondHasPassed") {
if (Waiting === false && Cooldown === false) {
Waiting = true
SentTime = Date.now()
CurrentCount++
Context.clearRect(0, 100, Canvas.width, Canvas.height)
Context.fillText(`*dot* (${CurrentCount})`, 700 / 2, 700 / 2)
}
}
})
addEventListener("keydown", async function (Key) {
if (Key.code === "Space") {
if (Cooldown === false && Waiting == true) {
Cooldown == true
Waiting = false
Context.clearRect(0, 100, Canvas.width, Canvas.height)
Context.fillText(`Tap! (${Date.now() - SentTime}ms)`, 700 / 2, 700 / 2)
await new Promise(Resolve => setTimeout(Resolve, 2000))
Context.clearRect(0, 100, Canvas.width, Canvas.height)
await new Promise(Resolve => setTimeout(Resolve, (Math.floor(Math.random() * 6)) * 1000))
Cooldown = false
}
}
})