aboutsummaryrefslogtreecommitdiff
path: root/src/components
diff options
context:
space:
mode:
authorasketonim <anton.dubik33@gmail.com>2020-01-03 17:02:00 +0300
committerasketonim <anton.dubik33@gmail.com>2020-01-03 17:02:00 +0300
commitf03467dbdda7c142a990d10d09d6711c1345323a (patch)
treeaf92141b3260064c4c3ca6b4f457fd872e885461 /src/components
parent33dc86d661f4cac10a87804ba1bacf92e36c16ce (diff)
downloadchrono-cube-ui-f03467dbdda7c142a990d10d09d6711c1345323a.tar.gz
Add displayTime() function
Diffstat (limited to 'src/components')
-rw-r--r--src/components/Timer/Timer.js62
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;