Skip to content
Advertisement

why is my js script not displaying timer result?

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 countdown variable must be defined as let countdown = new Date().getHours(). There is a syntax error there.
  • Inside your setInterval function your now variable, if it is planning to get the current hour time it should be defined as var now = new Date().getHours();
  • Also you have to redefine the Number function because Number is 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;
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement