File tree Expand file tree Collapse file tree 4 files changed +23
-5
lines changed Expand file tree Collapse file tree 4 files changed +23
-5
lines changed Original file line number Diff line number Diff line change @@ -45,6 +45,7 @@ function MyTimer({ expiryTimestamp }) {
45
45
export default function App ( ) {
46
46
const time = new Date ( ) ;
47
47
time . setSeconds ( time . getSeconds ( ) + 600 ) ; // 10 minutes timer
48
+ // time.setMilliseconds(time.getMilliseconds() + 6500); // 6.5 seconds timer
48
49
return (
49
50
< div >
50
51
< MyTimer expiryTimestamp = { time } />
Original file line number Diff line number Diff line change @@ -56,10 +56,25 @@ export default function useTimer(settings) {
56
56
setExpiryTimestamp ( newExpiryTimestamp ) ;
57
57
}
58
58
59
- useEffect ( ( ) => {
60
- if ( Validate . expiryTimestamp ( expiryTimestamp ) ) {
59
+ function handleExtraMilliSeconds ( secondsValue , extraMilliSeconds ) {
60
+ setIsRunning ( true ) ;
61
+ intervalRef . current = setTimeout ( ( ) => {
62
+ intervalRef . current = undefined ;
61
63
setSeconds ( Time . getSecondsFromExpiry ( expiryTimestamp ) ) ;
62
64
start ( ) ;
65
+ } , extraMilliSeconds ) ;
66
+ }
67
+
68
+ useEffect ( ( ) => {
69
+ if ( Validate . expiryTimestamp ( expiryTimestamp ) ) {
70
+ const secondsValue = Time . getSecondsFromExpiry ( expiryTimestamp ) ;
71
+ const extraMilliSeconds = Math . floor ( ( secondsValue - Math . floor ( secondsValue ) ) * 1000 ) ;
72
+ setSeconds ( secondsValue ) ;
73
+ if ( extraMilliSeconds > 0 ) {
74
+ handleExtraMilliSeconds ( secondsValue , extraMilliSeconds ) ;
75
+ } else {
76
+ start ( ) ;
77
+ }
63
78
}
64
79
return clearIntervalRef ;
65
80
} , [ expiryTimestamp ] ) ;
Original file line number Diff line number Diff line change 1
1
export default class Time {
2
- static getTimeFromSeconds ( totalSeconds ) {
2
+ static getTimeFromSeconds ( secs ) {
3
+ const totalSeconds = Math . ceil ( secs ) ;
3
4
const days = Math . floor ( totalSeconds / ( 60 * 60 * 24 ) ) ;
4
5
const hours = Math . floor ( ( totalSeconds % ( 60 * 60 * 24 ) ) / ( 60 * 60 ) ) ;
5
6
const minutes = Math . floor ( ( totalSeconds % ( 60 * 60 ) ) / 60 ) ;
@@ -17,7 +18,7 @@ export default class Time {
17
18
const now = new Date ( ) . getTime ( ) ;
18
19
const milliSecondsDistance = expiry - now ;
19
20
if ( milliSecondsDistance > 0 ) {
20
- return Math . floor ( milliSecondsDistance / 1000 ) ;
21
+ return milliSecondsDistance / 1000 ;
21
22
}
22
23
return 0 ;
23
24
}
Original file line number Diff line number Diff line change @@ -33,6 +33,7 @@ module.exports = {
33
33
devServer : {
34
34
contentBase : path . join ( __dirname , 'dev-dist' ) ,
35
35
compress : true ,
36
- port : 9000
36
+ port : 9000 ,
37
+ disableHostCheck : true
37
38
}
38
39
}
You can’t perform that action at this time.
0 commit comments