Skip to content

Commit 28083dc

Browse files
committed
fix useTimer expire after resume
1 parent 176aecf commit 28083dc

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/useTimer.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ export default function useTimer(settings) {
1414
}
1515
}
1616

17+
function handleExpire() {
18+
clearIntervalRef();
19+
Validate.onExpire(onExpire) && onExpire();
20+
}
21+
1722
function start() {
1823
if (!intervalRef.current) {
1924
intervalRef.current = setInterval(() => {
2025
const secondsValue = Time.getSecondsFromExpiry(expiryTimestamp);
2126
if (secondsValue <= 0) {
22-
clearIntervalRef();
23-
Validate.onExpire(onExpire) && onExpire();
27+
handleExpire();
2428
}
2529
setSeconds(secondsValue);
2630
}, 1000);
@@ -33,7 +37,13 @@ export default function useTimer(settings) {
3337

3438
function resume() {
3539
if (!intervalRef.current) {
36-
intervalRef.current = setInterval(() => setSeconds((prevSeconds) => (prevSeconds - 1)), 1000);
40+
intervalRef.current = setInterval(() => setSeconds((prevSeconds) => {
41+
const secondsValue = prevSeconds - 1;
42+
if (secondsValue <= 0) {
43+
handleExpire();
44+
}
45+
return secondsValue;
46+
}), 1000);
3747
}
3848
}
3949

0 commit comments

Comments
 (0)