Is there a way to put some text on screen at the end of this countdown or a cake? this is a countdown for my B-Day and i really want it to show some text and/or a cake at the end for my b-day but I have no clue on how. I have tried
if (distance < 0) {
setInterval(x);
}
}
but that didn’t work with the code, instead it completely broke it.
The link to the officail code is https://dateclock.w3spaces.com/b-day.html
// Function to calculate the time remaining
function calculateTimeRemaining() {
var currentDate = new Date();
var targetDate = new Date("October 16, 2023 12:00:00");
var timeRemaining = targetDate - currentDate;
var years = Math.floor(timeRemaining / (1000 * 60 * 60 * 24 * 365));
var days = Math.floor((timeRemaining % (1000 * 60 * 60 * 24 * 365)) / (1000 * 60 * 60 * 24));
var hours = Math.floor((timeRemaining % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((timeRemaining % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((timeRemaining % (1000 * 60)) / 1000);
var milliseconds = Math.floor(timeRemaining % 1000);
return {
years: years,
days: days,
hours: hours,
minutes: minutes,
seconds: seconds,
milliseconds: milliseconds
};
}
// Function to update the countdown timer
function updateCountdown() {
var countdown = calculateTimeRemaining();
document.getElementById("years").textContent = countdown.years;
document.getElementById("days").textContent = countdown.days;
document.getElementById("hours").textContent = countdown.hours;
document.getElementById("minutes").textContent = countdown.minutes;
document.getElementById("seconds").textContent = countdown.seconds;
document.getElementById("milliseconds").textContent = countdown.milliseconds;
setTimeout(updateCountdown, 1);
}
// Call the updateCountdown function when the page is loaded
window.onload = updateCountdown;
<center>
<h1> Countdown to Oct. 16, 2023 12:00PM </h1>
<h2> Days until my B-Day </h2>
<h1>Countdown Timer</h1>
<div>
<span id="years"></span> years |
<span id="days"></span> days |
<span id="hours"></span> hours |
<span id="minutes"></span> minutes |
<span id="seconds"></span> seconds |
<span id="milliseconds"></span> milliseconds
</div>
<a href="javascript:location.reload(true)">Refresh this page</a>
</center>
3
Answers
You can
updateCountdown()
check if thetimeRemaining
(which you’ll also have to return fromcalculateTimeRemaining
is less than or equal to 0. If this condition is true, then get the element with the id "cake" and update the text there, else whatever you have in the code works as usual.(I’m new to answering questions on here, so sorry if it was expected in a specified format)
updateCountdown
appears to be your function that keeps repeating. (Though I highly recommend a less brute-force interval than 1ms. The browser can’t re-draw and, more to the point, the human eye can’t perceive 1000 frames per second.) It sounds like you just want to conditionally perform that repetition vs. some other action. For example:So then what is
someCondition
? It looks likecountdown
is an object with several values on it. You could test all of those values individually, for example:That looks a bit clunky though. But you could add the total time to the returned object as well:
And just check that in the condition:
Then it’s up to you what you want to do when that condition is met. "Show some text and/or a cake" sounds like you want to have some HTML that isn’t visible and then make it visible. For example, you could have this:
And in your condition you could show it:
You can continue to customize from there, creating and modifying your HTML as you see fit.
Use setInterval and test the timeRemaining
I set the hidden attribute from a div I made and clear the interval to stop the processing
Also your HTML was invalid. All content tags belong in the body.
Lastly is
<center>
deprecated but let’s do that another time