I am coding a digital clock. The buttons purpose is to hide seconds if the user wants only see the hours and minutes but not the seconds, so the button should be hide the seconds on click. If the button is clicked "secs" will be hide but only until the new interval pass is in turn. I want that the "secs" will even be hided when the next interval pass is in turn until the user want to display the seconds. It is quite though for me to implement this, how can I?
window = onload = function () {
function displayTime(dataObject) {
const displayDate = document.getElementById('date-elements');
const displayTime = document.getElementById('time-elements');
const parts = {
//date elements:
weekday: dataObject.getDay(),
daysNumber: dataObject.getDate(),
month: dataObject.getMonth() + 1,
year: dataObject.getFullYear(),
//time elements:
hours: dataObject.getHours(),
minutes: dataObject.getMinutes(),
seconds: dataObject.getSeconds(),
};
const formatAMPM = parts.hours >= 12 ? "PM" : "AM";
const dayState = formatAMPM;
parts.hours = parts.hours % 12;
parts.hours ? parts.hours : 12;
//appending zero if smaller than 10
dysNmbr = parts.daysNumber < 10 ? "0" + parts.daysNumber : parts.daysNumber;
mnth = parts.month < 10 ? "0" + parts.month : parts.month;
hrs = parts.hours < 10 ? "0" + parts.hours : parts.hours;
mins = parts.minutes < 10 ? "0" + parts.minutes : parts.minutes;
secs = parts.seconds < 10 ? "0" + parts.seconds : parts.seconds;
//array of weekdays names
days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
//determine the weekday
currentDayName = days[parts.weekday];
displayDate.innerHTML = `${currentDayName}, ${dysNmbr}.${mnth}.${parts.year}`;
displayTime.innerHTML = `${hrs} : ${mins} : ${secs} ${dayState}`;
const btn = document.getElementById("btn");
btn.addEventListener("click", () => {
displayTime.innerHTML = `${hrs} : ${mins} ${dayState}`
})
}
let currentDate = new Date();
let formattedDate = displayTime(currentDate);
setInterval(() => {
currentDate = new Date();
formattedDate = displayTime(currentDate);
formattedDate;
}, 10);
};
<body>
<div id = "date-elements"></div>
<div id = "time-elements"></div>
<button id = "btn">Display seconds</button>
</body>