Skip to content

Commit 6406ef8

Browse files
Add playing flag and start timer when key is pressed
1 parent 2d74604 commit 6406ef8

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

03-midu-typing-game/index.html

+16-10
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ <h3 id="results-accuracy"></h3>
177177

178178
let words = []
179179
let currentTime = INITIAL_TIME
180+
let playing
180181

181182
initGame()
182183
initEvents()
@@ -186,6 +187,8 @@ <h3 id="results-accuracy"></h3>
186187
$results.style.display = 'none'
187188
$input.value = ''
188189

190+
playing = false
191+
189192
words = INITIAL_WORDS.toSorted(
190193
() => Math.random() - 0.5
191194
).slice(0, 50)
@@ -208,25 +211,28 @@ <h3 id="results-accuracy"></h3>
208211
const $firstWord = $paragraph.querySelector('word')
209212
$firstWord.classList.add('active')
210213
$firstWord.querySelector('letter').classList.add('active')
211-
212-
const intervalId = setInterval(() => {
213-
currentTime--
214-
$time.textContent = currentTime
215-
216-
if (currentTime === 0) {
217-
clearInterval(intervalId)
218-
gameOver()
219-
}
220-
}, 1000)
221214
}
222215

223216
function initEvents() {
224217
document.addEventListener('keydown', () => {
225218
$input.focus()
219+
if (!playing) {
220+
playing = true
221+
const intervalId = setInterval(() => {
222+
currentTime--
223+
$time.textContent = currentTime
224+
225+
if (currentTime === 0) {
226+
clearInterval(intervalId)
227+
gameOver()
228+
}
229+
}, 1000)
230+
}
226231
})
227232
$input.addEventListener('keydown', onKeyDown)
228233
$input.addEventListener('keyup', onKeyUp)
229234
$button.addEventListener('click', initGame)
235+
230236
}
231237

232238
function onKeyDown(event) {

0 commit comments

Comments
 (0)