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 hidden or 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); | |
})(); |
@schalkneethling If you happen to be familiar with Alpine JS—based on Vue—do you have any ideas how to adapt this to Alpine?
This video got me started, but I'm struggling with how to integrate an anonymous function which responds to essentially an onload() event and not a click event.
Zero pressure, but if you happen to have any ideas on how to solve this, I would love to know!
Hey @ctessmer - I have not used Alpine, but if the primary problem is the anonymous function you can turn this into a named function. The primary reason this was written as is, is so that it will start as soon as the DOM is "ready" in a sense.
You could also do the following:
const startClock = () => {
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);
};
Wherever you want to kick it off, you can call startClock();
- I hope this helps.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So kind of you to respond! Thank you again for posting it. So elegant!