diff options
author | asketonim <anton.dubik33@gmail.com> | 2020-01-02 21:34:47 +0300 |
---|---|---|
committer | asketonim <anton.dubik33@gmail.com> | 2020-01-02 21:34:47 +0300 |
commit | 18c5cf0f72311d679ac125675cd8608b99e5d815 (patch) | |
tree | e66f58e1806fe2670521f779e2dbdb3b4bf7f695 /src/components | |
parent | b2dd58bd08fb5120dd74b8e1484970cc0a68b4d6 (diff) | |
download | chrono-cube-ui-18c5cf0f72311d679ac125675cd8608b99e5d815.tar.gz |
Replace keyPress event with keyUp
Diffstat (limited to 'src/components')
-rw-r--r-- | src/components/Timer/Timer.js | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/src/components/Timer/Timer.js b/src/components/Timer/Timer.js index e095902..b10936f 100644 --- a/src/components/Timer/Timer.js +++ b/src/components/Timer/Timer.js @@ -5,16 +5,16 @@ import styled from 'styled-components'; const Timer = () => { const SPACE = 32 const [time, setTime] = useState("00:00:00") - const [stopwatchStarted, setStopwatchStarted] = useState(false); + const [running, setRunning] = useState(false); const [timer, setTimer] = useState(0); let startingTime; - const handleKeyPress = event => { + const handleKeyUp = event => { if (event.keyCode === SPACE){ - if (!stopwatchStarted) { + if (!running) { startingTime = Date.now(); - setStopwatchStarted(true); + setRunning(true); setTimer(setInterval(() => setTime(() => { const timeGap = Math.floor((Date.now() - startingTime) / 10); let resultTime = ""; @@ -36,8 +36,9 @@ const Timer = () => { }), 10)) } else { clearInterval(timer) - setStopwatchStarted(false); + setRunning(false); startingTime = 0; + console.log(time) return false; } } @@ -45,10 +46,10 @@ const Timer = () => { useEffect(() => { - window.addEventListener("keypress", handleKeyPress); + window.addEventListener("keyup", handleKeyUp); return () => { - window.removeEventListener("keypress", handleKeyPress); + window.removeEventListener("keyup", handleKeyUp); }; }) |