I'm in the middle of a countdown app project (Pomodoro) The app doesn't work for me, something in the calculation of the Math library may not be written correctly even though I think it is written correctly.
I am attaching the piece of code that does not work and also the complete code of the entire application
this is all my code, CSS is not necessary
hope for help :))
let timer;
function startTimer() {
const hours = document.getElementById("hours").value;
const minutes = document.getElementById("minutes").value;
const seconds = document.getElementById("seconds").value;
let totalTime = (hours * 3600) + (minutes * 60) + (seconds); // sum all seconds
if (totalTime <= 0) {
alert('Please set a valid time before starting.');
return;
}
document.getElementById("input-section").style.display = 'none';
document.getElementById("display-section").style.display = 'flex';
console.log(timer);
document.title = ('Time To Go ' + timer);
if (timer) {
clearInterval(timer);
}
timer = setInterval(
function() {
if (totalTime <= 0) {
clearInterval(timer);
alert("Time's Up!!");
return;
}
const displayHours = Math.floor(totalTime / 3600);
const displayMinutes = Math.floor((totalTime % 3600) / 60);
const displaySeconds = totalTime % 60;
document.getElementById("displayHours").textContent = String(displayHours).padStart(2, '0');
document.getElementById("displayMinutes").textContent = String(displayMinutes).padStart(2, '0');
document.getElementById("displaySeconds").textContent = String(displaySeconds).padStart(2, '0');
totalTime--;
}, 1000);
}
function stopTimer() {
if (timer) {
clearInterval(timer);
}
document.getElementById("inputSection").style.display = 'flex';
document.getElementById("displaySection").style.display = 'none';
}
<div class="container">
<div class="header">Countdown Timer</div>
<div class="input-section" id="input-section">
<input type="number" id="hours" placeholder="hours" min="0">
<input type="number" id="minutes" placeholder="minutes" min="0" max="59">
<input type="number" id="seconds" placeholder="seconds" min="0" max="59">
<button onclick="startTimer()">Start</button>
</div>
<div class="display-section" id="display-section">
<span id="displayHours">00</span>
<span id="displayminutes">00</span>
<span id="displaySeconds">00</span>
<button onclick="stopTimer()">Stop</button>
</div>
</div>
my problem is counting the time or something with the Math.floor
In your HTML, you have an element with and
idof:displayminutes, but in your JavaScript, you try to get a reference to:displayMinutesand that causes thetextContenterror you are currently getting. Fixing that error will at least get the timer running.Beyond that, you are using some old techniques for setting up event handlers and you are using inline styles, which should always be avoided when possible. You also don't need separate start and stop buttons and separate functions for start and stop. You also aren't really using
flexhere, so that's not needed, just a single CSS class that can be applied when you want to hide something and removed when you want to show it.See the re-worked code below and read the comments for details.