From e41bdbcd96a567cac025174cdfcc1b5222e6644b Mon Sep 17 00:00:00 2001 From: Iz P Date: Wed, 10 Apr 2019 15:02:40 -0600 Subject: [PATCH 1/2] Created Reset Functionality for useTimer. --- src/App.js | 4 +++- src/useTimer.js | 19 ++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/App.js b/src/App.js index fc26383..7269ce2 100644 --- a/src/App.js +++ b/src/App.js @@ -9,7 +9,8 @@ function MyTimer({ expiryTimestamp }) { days, start, pause, - resume + resume, + restart } = useTimer({ expiryTimestamp, onExpire: () => console.warn('onExpire called') }); @@ -23,6 +24,7 @@ function MyTimer({ expiryTimestamp }) { + ); } diff --git a/src/useTimer.js b/src/useTimer.js index 9bf5106..e3e6215 100644 --- a/src/useTimer.js +++ b/src/useTimer.js @@ -105,7 +105,7 @@ export function useStopwatch(settings) { /* ---------------------- useTimer --------------------- */ export function useTimer(settings) { - const { expiryTimestamp, onExpire } = settings || {}; + let { expiryTimestamp, onExpire } = settings || {}; const [seconds, setSeconds] = useState(0); function subtractSecond() { @@ -164,7 +164,7 @@ export function useTimer(settings) { function start() { if(isValidExpiryTimestamp(expiryTimestamp) && !intervalRef.current) { calculateExpiryDate(); - intervalRef.current = setInterval(() => calculateExpiryDate(), 1000); + intervalRef.current = setInterval(() => subtractSecond(), 1000); } } @@ -192,6 +192,19 @@ export function useTimer(settings) { } } + function restart(offset) { + reset(); + // Calculate fresh expiry and start timer again with new expiry ammount + calcNewExpiry(offset); + start(); + } + + function calcNewExpiry(offset) { + let t = new Date(); + t.setSeconds(t.getSeconds() + offset); + expiryTimestamp = t; + } + // Timer expiry date calculation function calculateExpiryDate() { var now = new Date().getTime(); @@ -236,7 +249,7 @@ export function useTimer(settings) { return isValid; } - return { seconds, minutes, hours, days, start, pause, resume }; + return { seconds, minutes, hours, days, start, pause, resume, restart }; } From 91e8b7d2710be662221ca57b9ea56d2df2d61700 Mon Sep 17 00:00:00 2001 From: Iz P Date: Sat, 13 Apr 2019 16:30:28 -0600 Subject: [PATCH 2/2] Updates to implementation of Restart function --- src/App.js | 7 ++++++- src/useTimer.js | 16 +++++----------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/App.js b/src/App.js index 7269ce2..e6f5903 100644 --- a/src/App.js +++ b/src/App.js @@ -24,7 +24,12 @@ function MyTimer({ expiryTimestamp }) { - + ); } diff --git a/src/useTimer.js b/src/useTimer.js index e3e6215..264e343 100644 --- a/src/useTimer.js +++ b/src/useTimer.js @@ -105,7 +105,8 @@ export function useStopwatch(settings) { /* ---------------------- useTimer --------------------- */ export function useTimer(settings) { - let { expiryTimestamp, onExpire } = settings || {}; + const { expiryTimestamp: expiry, onExpire } = settings || {}; + const [expiryTimestamp, setExpiryTimestamp] = useState(expiry); const [seconds, setSeconds] = useState(0); function subtractSecond() { @@ -192,18 +193,11 @@ export function useTimer(settings) { } } - function restart(offset) { + function restart(newExpiryTimestamp) { reset(); - // Calculate fresh expiry and start timer again with new expiry ammount - calcNewExpiry(offset); - start(); + setExpiryTimestamp(newExpiryTimestamp); } - function calcNewExpiry(offset) { - let t = new Date(); - t.setSeconds(t.getSeconds() + offset); - expiryTimestamp = t; - } // Timer expiry date calculation function calculateExpiryDate() { @@ -228,7 +222,7 @@ export function useTimer(settings) { useEffect(() => { start(); return reset; - },[]); + },[expiryTimestamp]); // Validate expiryTimestamp