diff options
author | sanine-a <sanine.not@pm.me> | 2021-04-13 15:28:08 -0500 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2021-04-13 15:28:08 -0500 |
commit | 9207e74f3af48a7a0c76188401c7470c0331ce3e (patch) | |
tree | 5430fea9271ec5893190c82a9408120295b2cdc5 | |
parent | cd797721f07e3368f56d2f293d49e1ad37029cbb (diff) |
add timer
-rw-r--r-- | App.js | 21 |
1 files changed, 17 insertions, 4 deletions
@@ -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), ] ); |