diff --git a/app/App.js b/app/App.js index 6342fa4..20e03fc 100644 --- a/app/App.js +++ b/app/App.js @@ -26,7 +26,7 @@ function MyTimer({ expiryTimestamp }) { @@ -35,7 +35,7 @@ function MyTimer({ expiryTimestamp }) { } export default function App() { - var t = new Date(); + const t = new Date(); t.setSeconds(t.getSeconds() + 600); // 10 minutes timer return (
diff --git a/src/useTimer.js b/src/useTimer.js index 0480e80..c6d5209 100644 --- a/src/useTimer.js +++ b/src/useTimer.js @@ -135,6 +135,26 @@ export default function useTimer(settings) { setExpiryTimestamp(newExpiryTimestamp); } + + // Timer expiry date calculation + function calculateExpiryDate() { + const now = new Date().getTime(); + const distance = expiryTimestamp - now; + const days = Math.floor(distance / (1000 * 60 * 60 * 24)); + const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); + const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); + const seconds = Math.floor((distance % (1000 * 60)) / 1000); + if(seconds < 0) { + reset(); + isValidOnExpire(onExpire) && onExpire(); + } else { + setSeconds(seconds); + setMinutes(minutes); + setHours(hours); + setDays(days); + } + } + // didMount effect useEffect(() => { start();