Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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;
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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(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 = parseToNumber(hours);
  $minutes.innerHTML = parseToNumber(minutes);
  $second.innerHTML = parseToNumber(second);
}

function parseToNumber(num) {
  return num < 10 ? "0" + num : num;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading