Skip to content

Commit 97a830d

Browse files
committed
Time util calculation seconds ceil
1 parent b910485 commit 97a830d

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

src/useTimer.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ export default function useTimer(settings) {
5757
}
5858

5959
function handleExtraMilliSeconds(secondsValue, extraMilliSeconds) {
60-
setSeconds(Math.floor(secondsValue) + 1);
6160
intervalRef.current = setTimeout(() => {
6261
intervalRef.current = undefined;
6362
setSeconds(Time.getSecondsFromExpiry(expiryTimestamp));
@@ -69,10 +68,10 @@ export default function useTimer(settings) {
6968
if (Validate.expiryTimestamp(expiryTimestamp)) {
7069
const secondsValue = Time.getSecondsFromExpiry(expiryTimestamp);
7170
const extraMilliSeconds = ((secondsValue - Math.floor(secondsValue)) * 1000).toFixed(2);
71+
setSeconds(secondsValue);
7272
if (extraMilliSeconds > 0) {
7373
handleExtraMilliSeconds(secondsValue, extraMilliSeconds);
7474
} else {
75-
setSeconds(secondsValue);
7675
start();
7776
}
7877
}

src/utils/Time.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export default class Time {
2-
static getTimeFromSeconds(totalSeconds) {
2+
static getTimeFromSeconds(secs) {
3+
const totalSeconds = Math.ceil(secs);
34
const days = Math.floor(totalSeconds / (60 * 60 * 24));
45
const hours = Math.floor((totalSeconds % (60 * 60 * 24)) / (60 * 60));
56
const minutes = Math.floor((totalSeconds % (60 * 60)) / 60);
@@ -16,9 +17,8 @@ export default class Time {
1617
static getSecondsFromExpiry(expiry) {
1718
const now = new Date().getTime();
1819
const milliSecondsDistance = expiry - now;
19-
const roundedValue = Math.ceil((milliSecondsDistance) / 100) * 100;
20-
if (roundedValue > 0) {
21-
return roundedValue / 1000;
20+
if (milliSecondsDistance > 0) {
21+
return milliSecondsDistance / 1000;
2222
}
2323
return 0;
2424
}

0 commit comments

Comments
 (0)