@@ -105,7 +105,8 @@ export function useStopwatch(settings) {
105105/* ---------------------- useTimer --------------------- */
106106
107107export function useTimer ( settings ) {
108- const { expiryTimestamp, onExpire } = settings || { } ;
108+ const { expiryTimestamp : expiry , onExpire } = settings || { } ;
109+ const [ expiryTimestamp , setExpiryTimestamp ] = useState ( expiry ) ;
109110
110111 const [ seconds , setSeconds ] = useState ( 0 ) ;
111112 function subtractSecond ( ) {
@@ -164,7 +165,7 @@ export function useTimer(settings) {
164165 function start ( ) {
165166 if ( isValidExpiryTimestamp ( expiryTimestamp ) && ! intervalRef . current ) {
166167 calculateExpiryDate ( ) ;
167- intervalRef . current = setInterval ( ( ) => calculateExpiryDate ( ) , 1000 ) ;
168+ intervalRef . current = setInterval ( ( ) => subtractSecond ( ) , 1000 ) ;
168169 }
169170 }
170171
@@ -192,6 +193,12 @@ export function useTimer(settings) {
192193 }
193194 }
194195
196+ function restart ( newExpiryTimestamp ) {
197+ reset ( ) ;
198+ setExpiryTimestamp ( newExpiryTimestamp ) ;
199+ }
200+
201+
195202 // Timer expiry date calculation
196203 function calculateExpiryDate ( ) {
197204 var now = new Date ( ) . getTime ( ) ;
@@ -215,7 +222,7 @@ export function useTimer(settings) {
215222 useEffect ( ( ) => {
216223 start ( ) ;
217224 return reset ;
218- } , [ ] ) ;
225+ } , [ expiryTimestamp ] ) ;
219226
220227
221228 // Validate expiryTimestamp
@@ -236,7 +243,7 @@ export function useTimer(settings) {
236243 return isValid ;
237244 }
238245
239- return { seconds, minutes, hours, days, start, pause, resume } ;
246+ return { seconds, minutes, hours, days, start, pause, resume, restart } ;
240247}
241248
242249
0 commit comments