Created
July 8, 2022 15:23
-
-
Save schalkneethling/de2b103e3b9a9259f7a10477b943523d to your computer and use it in GitHub Desktop.
A simple JavaScript countdown timer
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
(function () { | |
dayjs.extend(window.dayjs_plugin_duration); | |
const launchDay = Date.parse("2022-06-10T21:00:00Z"); | |
const daysOutput = document.getElementById("countdown-days"); | |
const hoursOutput = document.getElementById("countdown-hours"); | |
const minutesOutput = document.getElementById("countdown-minutes"); | |
const secondsOutput = document.getElementById("countdown-seconds"); | |
let duration; | |
setInterval(() => { | |
duration = dayjs.duration(launchDay - Date.now()); | |
daysOutput.textContent = duration["$d"].days; | |
hoursOutput.textContent = | |
duration["$d"].hours > 9 | |
? duration["$d"].hours | |
: `0${duration["$d"].hours}`; | |
minutesOutput.textContent = | |
duration["$d"].minutes > 0 | |
? duration["$d"].minutes | |
: `0${duration["$d"].minutes}`; | |
secondsOutput.textContent = | |
duration["$d"].seconds > 9 | |
? duration["$d"].seconds | |
: `0${duration["$d"].seconds}`; | |
}, 100); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment