I’m making a countdown timer hours minutes and second but the script I’m running doesn’t display the results I wrote in the js script, can anyone correct my code so that the timer can work? The problem
let countdown = new date().getHours()
let $hours = document.getElementById('hours');
let $minutes = document.getElementById('minutes');
let $second = document.getElementById('second');
setInterval(function() {
var now = new hours();
var timeleft = (countdown - now) / 1000;
updateclock(timeleft);
}, 1000);
function updateclock(removebgtime) {
let hours = Math.floor((remainingtime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
let minutes = Math.floor((remainingtime % (1000 * 60 * 60)) / (1000 * 60))
let second = Math.floor((remainingtime % (1000 * 60)) / 1000);
$hours.innerHTML = Number(hours);
$minutes.innerHTML = Number(minutes);
$second.innerHTML = Number(second);
}
function Number(Number) {
return Number < 10 ? "0" + Number : Number;
}Advertisement
Answer
Issues that I found in code.
- Your
countdownvariable must be defined aslet countdown = new Date().getHours(). There is a syntax error there. - Inside your
setIntervalfunction yournowvariable, if it is planning to get the current hour time it should be defined asvar now = new Date().getHours(); - Also you have to redefine the
Numberfunction becauseNumberis a datatype in javascript
Pseudo Code.
let countdown = new Date().getHours()
let $hours = document.getElementById('hours');
let $minutes = document.getElementById('minutes');
let $second = document.getElementById('second');
setInterval(function() {
var now = new Date().getHours();
var timeleft = (countdown - now) / 1000;
updateclock(timeleft);
}, 1000);
function updateclock(remainingtime) {
let hours = Math.floor((remainingtime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
let minutes = Math.floor((remainingtime % (1000 * 60 * 60)) / (1000 * 60))
let second = Math.floor((remainingtime % (1000 * 60)) / 1000);
$hours.innerHTML = parseToNumber(hours);
$minutes.innerHTML = parseToNumber(minutes);
$second.innerHTML = parseToNumber(second);
}
function parseToNumber(num) {
return num < 10 ? "0" + num : num;
}