0

I've been working on a timer for a few weeks now. When I try to subtract minutes from 0, it goes into negatives without touching the hours. I've tried a few methods, read the documentation, and watched several YouTube videos, but none of them help me. Here's the code I used:

seconds--;
    if (seconds === 0) {
        seconds = 59;
        minutes--;
        if (minutes < 10) {
            minutesText.innerText = '0' + minutes.toString()
        } else {
            minutesText.innerText = minutes;
        }
        if (minutes === 0) {
            minutes = 59;
            hours--;
            if (hours < 10) {
                hoursText.innerText = '0' + hours.toString()
            } else {
                hoursText.innerText = hours;
            }
            if (hours === 0) {
                window.clearInterval(interval);
            }
        }

    }
    if (seconds < 10) {
        secondsText.innerText = '0' + seconds.toString();
    } else {
        secondsText.innerText = seconds;
    }

It never touches the hours-- and doesn't make minutes be equal to 59.

(Sorry if it's an easy fix, I'm just getting started on JS)

1
  • Hint: What does minutes-- do when minutes is 0? Will minutes === 0 catch that condition?
    – tadman
    Feb 5, 2021 at 1:33

1 Answer 1

1

You have a couple of problems:

  1. You're updating minutesText.innerText before you check whether minutes is 0 and thus needs to be changed to 59. It should be after the if (minutes === 0) block instead.
  2. You need to check if values are 0 before you decrement them, otherwise you will never catch the 0 value.

This snippet incorporates those fixes.

let hoursText = document.getElementById('hours');
let minutesText = document.getElementById('minutes');
let secondsText = document.getElementById('seconds');


function updateTimer() {
  hours = +hoursText.innerText;
  minutes = +minutesText.innerText;
  seconds = +secondsText.innerText;
  if (seconds === 0) {
    seconds = 59;
    if (minutes === 0) {
      minutes = 59;
      if (hours === 0) {
        window.clearInterval(interval);
        return;
      } else {
        hours--;
      }
      if (hours < 10) {
        hoursText.innerText = '0' + hours.toString()
      } else {
        hoursText.innerText = hours;
      }
    } else {
      minutes--;
    }
    if (minutes < 10) {
      minutesText.innerText = '0' + minutes.toString()
    } else {
      minutesText.innerText = minutes;
    }
  } else {
    seconds--;
  }
  if (seconds < 10) {
    secondsText.innerText = '0' + seconds.toString();
  } else {
    secondsText.innerText = seconds;
  }
}

interval = setInterval(updateTimer, 1000);
<div><span id="hours">00</span>:<span id="minutes">01</span>:<span id="seconds">13</span></div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.