9 Countdown Timer

Page section: playground
Blurb
Blurb
Blurb

Code for this project

The code for this project:
// Code for Countdown timer by Code With Bubb

const hoursInput = document.querySelector('#hours-input');
const minutesInput = document.querySelector('#minutes-input');
const secondsInput = document.querySelector('#seconds-input');
const hoursOutput = document.querySelector('.hours');
const minutesOutput = document.querySelector('.minutes');
const secondsOutput = document.querySelector('.seconds');
const startBtn = document.querySelector('.start-timer-btn');

// The user input time
let targetTime;
let timerInterval;

const updateTimer = () => {
  if (targetTime) {
    const differenceInSeconds = Math.floor(targetTime - Date.now()) / 1000;

    if (differenceInSeconds < 1) {
      clearInterval(timerInterval);
    }

    const hours = Math.floor(differenceInSeconds / 3600);
    const minutes = Math.floor(differenceInSeconds / 60) % 60;
    const seconds = Math.floor(differenceInSeconds % 60);

    hoursOutput.textContent = `${hours} hours`;
    minutesOutput.textContent = `${minutes} minutes`;
    secondsOutput.textContent = `${seconds} seconds`;
  }
};

startBtn.addEventListener('click', () => {
  const futureHours = parseInt(hoursInput.value);
  const futureMinutes = parseInt(minutesInput.value);
  const futureSeconds = parseInt(secondsInput.value);

  const date = new Date();

  const currentHours = date.getHours();
  const currentMinutes = date.getMinutes();
  const currentSeconds = date.getSeconds();

  date.setHours(currentHours + futureHours);
  date.setMinutes(currentMinutes + futureMinutes);
  date.setSeconds(currentSeconds + futureSeconds);

  targetTime = date.getTime();
  localStorage.setItem('targetTime', targetTime);

  updateTimer();
  timerInterval = setInterval(updateTimer, 500);
});

const storedTargetTime = localStorage.getItem('targetTime');

if (storedTargetTime) {
  targetTime = storedTargetTime;
  updateTimer();
  timerInterval = setInterval(updateTimer, 500);
}
View contents of: /staticcountdown.js