Last active
February 7, 2022 21:04
-
-
Save nicovalencia/75a5ac2cd22b1a384962abd25d29b741 to your computer and use it in GitHub Desktop.
React Countdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const [countdown, setCountdown] = useState('xx:xx') | |
function updateCountdown() { | |
const totalSeconds = (TARGET_TIMESTAMP - new Date().getTime()) / 1000 | |
const minutes = Math.floor(totalSeconds / 60) | |
const seconds = Math.round(totalSeconds % 60) | |
const countdown = `${minutes}:${seconds > 9 ? seconds : '0' + seconds}` | |
if (totalSeconds > 1) { | |
setCountdown(countdown) | |
setTimeout(updateCountdown, 1000) | |
} else { | |
setCountdown('Reload!') | |
} | |
} | |
useEffect(() => { | |
updateCountdown() | |
}, []) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment