Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function MyTimer({ expiryTimestamp }) {
<button onClick={resume}>Resume</button>
<button onClick={() => {
// Restarts to 5 minutes timer
var t = new Date();
const t = new Date();
t.setSeconds(t.getSeconds() + 300);
restart(t)
}}>restart</button>
Expand All @@ -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 (
<div>
Expand Down
20 changes: 20 additions & 0 deletions src/useTimer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down