From 9207e74f3af48a7a0c76188401c7470c0331ce3e Mon Sep 17 00:00:00 2001 From: sanine-a Date: Tue, 13 Apr 2021 15:28:08 -0500 Subject: add timer --- App.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/App.js b/App.js index 3658c8b..d7dc14b 100644 --- a/App.js +++ b/App.js @@ -26,6 +26,7 @@ const GAMEMODE_ALL_TO_LEVEL = 2; let state = { level: 0, gameMode: GAMEMODE_LEVEL_ONLY, + paused: false, levelCharacters: [], index: 0, @@ -58,7 +59,7 @@ function setState(update) const App = function() { const { - levelCharacters, + levelCharacters, time, level, gameMode, character, pinyin, numGuesses, correctGuesses, @@ -74,7 +75,7 @@ const App = function() numCharacters: levelCharacters.length, guesses: numGuesses, correctGuesses, - time: 0, + time, }), h('h1', {}, character), @@ -221,10 +222,19 @@ function reset() { const levelCharacters = buildLevel(level, LEVEL_SIZE, gameMode, characters); const [character, pinyin] = levelCharacters[0]; + + clearInterval(timeInterval); + timeInterval = setInterval(() => { + const { paused, time } = state; + if (!paused) + setState({ time: time+1 }); + }, 1000); setState({ levelCharacters, index: 0, + paused: false, + time: 0, character, pinyin, @@ -243,10 +253,13 @@ function reset() { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +let timeInterval; + function ScoreView({ level, numCharacters, guesses, correctGuesses, time }) { const minutes = Math.floor(time/60); - const seconds = time % 60; + let seconds = time % 60; + seconds = seconds < 10 ? '0' + seconds : seconds; const levelName = `Level ${level + 1}`; @@ -257,7 +270,7 @@ function ScoreView({ level, numCharacters, guesses, correctGuesses, time }) return h( 'div', {}, [ - h('h1', {}, levelName), + h('h1', {}, `${levelName} - ${minutes}:${seconds}`), h('h1', {}, score), ] ); -- cgit v1.2.1