diff options
author | Eugene <eug-vs@keemail.me> | 2020-01-16 19:32:18 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-16 19:32:18 +0000 |
commit | 22918f53bdee82e0a34bb2a09059ccd3ffca0dc6 (patch) | |
tree | 52779dc13ae2b8714b108c6cd23951de36344fbd /src/pages/Timer/TimerButton | |
parent | 8a005dd6d506faf4fd1d5766a3f51d94300f4fd8 (diff) | |
parent | 6ceb6dbb3a7a43394a2318c8c9eda44e8c7a3ccb (diff) | |
download | chrono-cube-ui-22918f53bdee82e0a34bb2a09059ccd3ffca0dc6.tar.gz |
Merge pull request #40 from Eug-VS/eslint
feat: configure ESlint
Diffstat (limited to 'src/pages/Timer/TimerButton')
-rw-r--r-- | src/pages/Timer/TimerButton/TimerButton.js | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/src/pages/Timer/TimerButton/TimerButton.js b/src/pages/Timer/TimerButton/TimerButton.js index 76bb65d..0f43c5a 100644 --- a/src/pages/Timer/TimerButton/TimerButton.js +++ b/src/pages/Timer/TimerButton/TimerButton.js @@ -1,7 +1,7 @@ import React, { useState, useEffect } from 'react'; import { Paper, Typography } from '@material-ui/core'; -import { makeStyles } from "@material-ui/core/styles"; +import { makeStyles } from '@material-ui/core/styles'; const useStyles = makeStyles(theme => ({ root: { @@ -19,21 +19,25 @@ const TimerButton = ({ registerResult }) => { const maxCountdown = 15000; const [time, setTime] = useState('00:00:00'); const [mode, setMode] = useState('idle'); - const [repeater, setRepeater] = useState(0); useEffect(()=> { - clearInterval(repeater); const timestamp = Date.now(); - if (mode === 'countdown') setRepeater(setInterval(() => { - const timeDelta = maxCountdown - (Date.now() - timestamp); - if (timeDelta <= 0) setMode('over'); - setTime(convertTimeToString(timeDelta)); - }, 10)); + if (mode === 'countdown') { + const repeater = setInterval(() => { + const timeDelta = maxCountdown - (Date.now() - timestamp); + if (timeDelta <= 0) setMode('over'); + setTime(convertTimeToString(timeDelta)); + }, 10); + return () => clearInterval(repeater); + } - if (mode === 'running') setRepeater(setInterval(() => { - setTime(convertTimeToString(Date.now() - timestamp)); - }, 10)); + if (mode === 'running') { + const repeater = setInterval(() => { + setTime(convertTimeToString(Date.now() - timestamp)); + }, 10); + return () => clearInterval(repeater); + } if (mode === 'over') { setTime('00:00:00'); @@ -46,7 +50,6 @@ const TimerButton = ({ registerResult }) => { }; const handleKeyUp = event => { - clearInterval(repeater); if (event.keyCode === SPACE) { if (mode === 'running') { registerResult(time); |