@@ -5,6 +5,7 @@ export default function useTimer(settings) {
5
5
const { expiryTimestamp : expiry , onExpire } = settings || { } ;
6
6
const [ expiryTimestamp , setExpiryTimestamp ] = useState ( expiry ) ;
7
7
const [ seconds , setSeconds ] = useState ( Time . getSecondsFromExpiry ( expiryTimestamp ) ) ;
8
+ const [ isRunning , setIsRunning ] = useState ( true ) ;
8
9
const intervalRef = useRef ( ) ;
9
10
10
11
function clearIntervalRef ( ) {
@@ -16,11 +17,13 @@ export default function useTimer(settings) {
16
17
17
18
function handleExpire ( ) {
18
19
clearIntervalRef ( ) ;
20
+ setIsRunning ( false ) ;
19
21
Validate . onExpire ( onExpire ) && onExpire ( ) ;
20
22
}
21
23
22
24
function start ( ) {
23
25
if ( ! intervalRef . current ) {
26
+ setIsRunning ( true ) ;
24
27
intervalRef . current = setInterval ( ( ) => {
25
28
const secondsValue = Time . getSecondsFromExpiry ( expiryTimestamp ) ;
26
29
if ( secondsValue <= 0 ) {
@@ -32,11 +35,13 @@ export default function useTimer(settings) {
32
35
}
33
36
34
37
function pause ( ) {
38
+ setIsRunning ( false ) ;
35
39
clearIntervalRef ( ) ;
36
40
}
37
41
38
42
function resume ( ) {
39
43
if ( ! intervalRef . current ) {
44
+ setIsRunning ( true ) ;
40
45
intervalRef . current = setInterval ( ( ) => setSeconds ( ( prevSeconds ) => {
41
46
const secondsValue = prevSeconds - 1 ;
42
47
if ( secondsValue <= 0 ) {
@@ -62,6 +67,6 @@ export default function useTimer(settings) {
62
67
63
68
64
69
return {
65
- ...Time . getTimeFromSeconds ( seconds ) , start, pause, resume, restart,
70
+ ...Time . getTimeFromSeconds ( seconds ) , start, pause, resume, restart, isRunning ,
66
71
} ;
67
72
}
0 commit comments