diff options
-rw-r--r-- | src/components/Timer/Timer.js | 62 |
1 files changed, 22 insertions, 40 deletions
diff --git a/src/components/Timer/Timer.js b/src/components/Timer/Timer.js index fea0ed2..1a8f6e8 100644 --- a/src/components/Timer/Timer.js +++ b/src/components/Timer/Timer.js @@ -30,27 +30,7 @@ const Timer = () => { return; } - setTime(() => { - const timeLeft = Math.floor((countdownInMills - timeGap) / 10); - let resultTime = ""; - - const minute = Math.floor(timeLeft / 6000); - if (minute < 10) resultTime += '0'; - resultTime += minute + ':'; - - let second = Math.floor(timeLeft / 100); - if (second < 10) resultTime += '0'; - if (second > 59) second %= 60; - resultTime += second + ':'; - - const mill = timeLeft % 100; - if (mill < 10) resultTime += '0'; - resultTime += mill; - - return resultTime; - }) - - + setTime(displayTime(countdownInMills - timeGap)); } } @@ -61,25 +41,7 @@ const Timer = () => { if (!running) { startingTime = Date.now(); setRunning(true); - setTimer(setInterval(() => setTime(() => { - const timeGap = Math.floor((Date.now() - startingTime) / 10); - let resultTime = ""; - - const minute = Math.floor(timeGap / 6000); - if (minute < 10) resultTime += '0'; - resultTime += minute + ':'; - - let second = Math.floor(timeGap / 100); - if (second < 10) resultTime += '0'; - if (second > 59) second %= 60 - resultTime += second + ':'; - - const mill = timeGap % 100; - if (mill < 10) resultTime += '0'; - resultTime += mill; - - return resultTime; - }), 10)) + setTimer(setInterval(() => setTime(displayTime((Date.now() - startingTime))), 10)); } else { clearInterval(timer); setRunning(false); @@ -119,4 +81,24 @@ const Root = styled.div` color: pink; `; +const displayTime = timeDiff => { + const timeGap = Math.floor(timeDiff / 10); + let resultTime = ""; + + const minute = Math.floor(timeGap / 6000); + if (minute < 10) resultTime += '0'; + resultTime += minute + ':'; + + let second = Math.floor(timeGap / 100); + if (second < 10) resultTime += '0'; + if (second > 59) second %= 60 + resultTime += second + ':'; + + const mill = timeGap % 100; + if (mill < 10) resultTime += '0'; + resultTime += mill; + + return resultTime; +}; + export default Timer; |