Skip to content

Commit b910485

Browse files
committed
support millisecond accuracy and minor enhancement
1 parent c3e834f commit b910485

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

src/useTimer.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,25 @@ export default function useTimer(settings) {
5656
setExpiryTimestamp(newExpiryTimestamp);
5757
}
5858

59-
useEffect(() => {
60-
if (Validate.expiryTimestamp(expiryTimestamp)) {
59+
function handleExtraMilliSeconds(secondsValue, extraMilliSeconds) {
60+
setSeconds(Math.floor(secondsValue) + 1);
61+
intervalRef.current = setTimeout(() => {
62+
intervalRef.current = undefined;
6163
setSeconds(Time.getSecondsFromExpiry(expiryTimestamp));
6264
start();
65+
}, extraMilliSeconds);
66+
}
67+
68+
useEffect(() => {
69+
if (Validate.expiryTimestamp(expiryTimestamp)) {
70+
const secondsValue = Time.getSecondsFromExpiry(expiryTimestamp);
71+
const extraMilliSeconds = ((secondsValue - Math.floor(secondsValue)) * 1000).toFixed(2);
72+
if (extraMilliSeconds > 0) {
73+
handleExtraMilliSeconds(secondsValue, extraMilliSeconds);
74+
} else {
75+
setSeconds(secondsValue);
76+
start();
77+
}
6378
}
6479
return clearIntervalRef;
6580
}, [expiryTimestamp]);

src/utils/Time.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ export default class Time {
1616
static getSecondsFromExpiry(expiry) {
1717
const now = new Date().getTime();
1818
const milliSecondsDistance = expiry - now;
19-
if (milliSecondsDistance > 0) {
20-
return Math.floor(milliSecondsDistance / 1000);
19+
const roundedValue = Math.ceil((milliSecondsDistance) / 100) * 100;
20+
if (roundedValue > 0) {
21+
return roundedValue / 1000;
2122
}
2223
return 0;
2324
}

webpack.dev.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ module.exports = {
3333
devServer: {
3434
contentBase: path.join(__dirname, 'dev-dist'),
3535
compress: true,
36-
port: 9000
36+
port: 9000,
37+
disableHostCheck: true
3738
}
3839
}

0 commit comments

Comments
 (0)