FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

End the game on "a" or "l" only · jackcoolpython/learning-area@3bb5063 · GitHub

Repository navigation

Commit 3bb5063

Browse files
authored
End the game on "a" or "l" only
Based on the description on MDN, pressing a wrong key shouldn't end the game, right? > Regardless of which one of the player control keys was pressed, remove the `keydown` event listener using `removeEventListener()` so that once the winning press has happened, no more keyboard input is possible to mess up the final game result.
1 parent 01ca141 commit 3bb5063

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

‎javascript/asynchronous/loops-and-intervals/reaction-game.html‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,21 @@
202202
document.addEventListener('keydown', keyHandler);
203203

204204
function keyHandler(e) {
205+
let isOver = false;
205206
console.log(e.key);
206-
if(e.key === "a") {
207+
208+
if (e.key === "a") {
207209
result.textContent = 'Player 1 won!!';
208-
} else if(e.key === "l") {
210+
isOver = true;
211+
} else if (e.key === "l") {
209212
result.textContent = 'Player 2 won!!';
213+
isOver = true;
210214
}
211215

212-
document.removeEventListener('keydown', keyHandler);
213-
setTimeout(reset, 5000);
216+
if (isOver) {
217+
document.removeEventListener('keydown', keyHandler);
218+
setTimeout(reset, 5000);
219+
}
214220
};
215221
}
216222
</script>

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL