I hope you are doing great. I want to toggle back and forth b/w "Time left to my birthday" Countdown Timer and "Time left to my New Year" Count down timer using onclick button method. The code works perfectly fine. All I need is when I click on the button "Time left to my birthday" the timer should be changed and vice versa. Is there any way that I can achieve this functionality?. Your help is always appreciated. Here is my code
const newYears = "18 Aug 2022";
const daysEl = document.getElementById("days");
const hoursEl = document.getElementById("hours");
const minsEl = document.getElementById("minutes");
const secsEl = document.getElementById("seconds");
function countdown() {
const newYearsDate = new Date(newYears);
const currentDate = new Date();
const totalSeconds = (newYearsDate - currentDate) / 1000;
const days = Math.floor(totalSeconds / 3600 / 24);
const hours = Math.floor(totalSeconds / 3600) % 24;
const minutes = Math.floor(totalSeconds / 60) % 60;
const seconds = Math.floor(totalSeconds) % 60;
daysEl.innerHTML = days;
hoursEl.innerHTML = formate(hours);
minsEl.innerHTML = formate(minutes);
secsEl.innerHTML = formate(seconds);
}
function formate(time) {
if (time < 10) {
return (`0${time}`);
} else {
return time
}
}
countdown()
setInterval(countdown, 1000)
* {
box-sizing: border-box;
text-transform: capitalize;
}
body {
font-family: "Lucida Sans", "Lucida Sans Regular", "Lucida Grande", "Lucida Sans Unicode", Geneva, Verdana, sans-serif;
height: 100vh;
margin: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
h1 {
font-weight: normal;
font-size: 2.5rem;
margin-top: -8rem;
}
.countdown-container {
display: flex;
}
.big-text {
font-weight: bold;
font-size: 4rem;
line-height: 1;
margin: 0 1.5rem;
}
.countdown-el {
text-align: center;
}
.countdown-el span {
font-size: 0.8rem;
}
.button {
top: 2rem;
display: inline-block;
border: 0;
outline: 0;
padding: 12px 16px;
line-height: 1.4;
background: linear-gradient(#4d4d4d, #2f2f2f);
border-radius: 5px;
border: 1px solid black;
font-family: "Lucida Grande", "Lucida Sans Unicode", Tahoma, Sans-Serif;
color: white !important;
font-size: 1.2em;
cursor: pointer;
/* Important part */
position: relative;
transition: padding-right 0.3s ease-out;
}
<h1>Time left To My next Birthday </h1>
<div class="countdown-container">
<div class="countdown-el days-c">
<p class="big-text" id="days">0</p>
<span>days</span>
</div>
<div class="countdown-el hours-c">
<p class="big-text" id="hours">0</p>
<span>hours</span>
</div>
<div class="countdown-el mins-c">
<p class="big-text" id="minutes">0</p>
<span>minutes</span>
</div>
<div class="countdown-el sec-c">
<p class="big-text" id="seconds">0</p>
<span>seconds</span>
</div>
</div>
<button class="button" onclick="">Time left to new year</button>
For that you could use an additional var for the toggle state (here birthday
) and one for the processed_date
and assign the date (birthday or next year) to it:
const nextBirthday = "18 Aug 2022";
const newYear = "01 Jan 2022";
let birthday = true;
let processed_date = nextBirthday;
...
function countdown() {
const newYearsDate = new Date(processed_date);
...
}
In the event handler you only need to toggle processed_date
and the textContent
of the heading and the button depending on the toggle state and invert the toggle state after that:
toggle_button.addEventListener('click', function() {
processed_date = birthday ? newYear : nextBirthday;
heading.querySelector('span').textContent = birthday ? 'new year' : 'my next birthday';
toggle_button.querySelector('span').textContent = birthday ? 'my birthday' : 'new year';
birthday = !birthday;
});
Working example: (smaller and without negative margin for demonstration)
const nextBirthday = "18 Aug 2022";
const newYear = "01 Jan 2022";
let processed_date = nextBirthday;
let birthday = true;
const daysEl = document.getElementById("days");
const hoursEl = document.getElementById("hours");
const minsEl = document.getElementById("minutes");
const secsEl = document.getElementById("seconds");
const heading = document.querySelector('h1');
const toggle_button = document.querySelector("button");
function countdown() {
const newYearsDate = new Date(processed_date);
const currentDate = new Date();
const totalSeconds = (newYearsDate - currentDate) / 1000;
const days = Math.floor(totalSeconds / 3600 / 24);
const hours = Math.floor(totalSeconds / 3600) % 24;
const minutes = Math.floor(totalSeconds / 60) % 60;
const seconds = Math.floor(totalSeconds) % 60;
daysEl.innerHTML = days;
hoursEl.innerHTML = formate(hours);
minsEl.innerHTML = formate(minutes);
secsEl.innerHTML = formate(seconds);
}
function formate(time) {
if (time < 10) {
return (`0${time}`);
} else {
return time
}
}
toggle_button.addEventListener('click', function() {
processed_date = birthday ? newYear : nextBirthday;
heading.querySelector('span').textContent = birthday ? 'new year' : 'my next birthday';
toggle_button.querySelector('span').textContent = birthday ? 'my next birthday' : 'new year';
birthday = !birthday;
countdown(); //not necessary - only for imidiate change instead of waiting 1 second
});
countdown();
setInterval(countdown, 1000);
* {
box-sizing: border-box;
text-transform: capitalize;
font-size: 10px;
}
body {
font-family: "Lucida Sans", "Lucida Sans Regular", "Lucida Grande", "Lucida Sans Unicode", Geneva, Verdana, sans-serif;
height: 100vh;
margin: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
h1 {
font-weight: normal;
font-size: 2.5rem;
}
.countdown-container {
display: flex;
}
.big-text {
font-weight: bold;
font-size: 4rem;
line-height: 1;
margin: 0 1.5rem;
}
.countdown-el {
text-align: center;
}
.countdown-el span {
font-size: 0.8rem;
}
.button {
top: 2rem;
display: inline-block;
border: 0;
outline: 0;
padding: 12px 16px;
line-height: 1.4;
background: linear-gradient(#4d4d4d, #2f2f2f);
border-radius: 5px;
border: 1px solid black;
font-family: "Lucida Grande", "Lucida Sans Unicode", Tahoma, Sans-Serif;
color: white !important;
font-size: 1.2em;
cursor: pointer;
/* Important part */
position: relative;
transition: padding-right 0.3s ease-out;
}
span {
font-size: inherit;
}
<h1>Time left To <span>My next Birthday</span></h1>
<div class="countdown-container">
<div class="countdown-el days-c">
<p class="big-text" id="days">0</p>
<span>days</span>
</div>
<div class="countdown-el hours-c">
<p class="big-text" id="hours">0</p>
<span>hours</span>
</div>
<div class="countdown-el mins-c">
<p class="big-text" id="minutes">0</p>
<span>minutes</span>
</div>
<div class="countdown-el sec-c">
<p class="big-text" id="seconds">0</p>
<span>seconds</span>
</div>
</div>
<button class="button">Time left to <span>new year</span></button>