skip to Main Content
let second = 2 // Make more realistic instead of 60
let minute = 1
let mytimer = setInterval(function(){
  let timer = second--
  console.log(minute + ':' + timer)
  if(second==0){
    second = 59
    minute--
  }
  if(minute==0){
    clearInterval(mytimer);
  }
},10)

As you can see, it’s quite simple code, but there is a weird bug in there. my timer doesn’t count second 0. When the timer gonna end, it’ll stop at 0:01. it works perfectly by itself, but when it reaches 1:00 or 0:00 it entirely ignores 0 and jumps from 1:01 to 0:59.
I also tried this solution, but it doesn’t work too.
what do I suppose to do now?
(btw my previous questions weren’t good enough, and I hope I could’ve to reach a better explanation this time)

3

Answers


  1. The issue is that your function is executed every 10 milliseconds, so when the timer reaches 1:00, it will skip the second zero and immediately jump to 0:59. To fix this ‘issue’, you can change the interval time from 10 milliseconds to 1000 milliseconds so it will display the 1:00 correctly.
    To do that, you have to replace the 10 at the end.

    Login or Signup to reply.
  2. This one is giving me the answer which you are looking for,

    let second = 59 // Make more realistic instead of 60
    let minute = 1
    let mytimer = setInterval(function(){
      let timer = second--
      console.log(minute + ':' + timer)
      if(second<0){
        second = 59
        minute--
      }
      if(minute==0){
        clearInterval(mytimer);
      }
    },10)
    Login or Signup to reply.
  3. Your code is stopping when min is 0, not when min and sec is zero. You need to change up your logic to account for that.

    let second = 0;
    let minute = 2;
    
    let mytimer = setInterval(function() {
      second--;
      if (second === 0 && minute === 0) {
        clearInterval(mytimer);
      } else if (second < 0) {
        second = 60 + second;
        minute--;
      }
    
      console.log(minute + ':' + second.toString().padStart(2, '0'))
    
    }, 1000)

    Now setInterval is NOT accurate. It will float. So it is better to use the a timestamp and get the difference.

    function msToTime(ms) {
      const seconds = Math.floor((ms / 1000) % 60).toString().padStart(2, '0');
      const minutes = Math.floor((ms / (1000 * 60)) % 60).toString().padStart(2, '0');
      
      return `${minutes}:${seconds}`;
    }
    
    const countDownTime = (outElem, ms) => {
      const endTS = Date.now() + ms;
      let timer;
      
      const display = text => outElem.textContent = text;
    
      const countDown = () => {
        const diff = endTS - Date.now();
        if (diff < 0) {
          display("00:00");
          window.clearTimeout(timer);
          return;
        }
    
        const ts = msToTime(diff);
        display(ts);
      };
    
      countDown();
      window.setInterval(countDown, 10);
    
    };
    
    countDownTime(document.querySelector('#out'), 2*60*1000);
    <div id="out"></div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search