-2

The minuites and hour dont seem to be correct, I need he minute to not be more than 60. Thanks a lot

 counter = 0;
    var timeleft = 36070;
    
    function leftPad(number, targetLength) {
      var output = number + '';
      while (output.length < targetLength) {
          output = '0' + output;
      }
      return output;
    }
    
    function covertSeconds(s){
      var hrs = Math.floor(s/3600);
      var min = Math.floor(s/60);
      var sec = s% 60;
      return leftPad(hrs,2) +":" + leftPad(min,2) + ":"+ leftPad(sec,2)
    }
    
     var timer = $("#timer").text(covertSeconds(timeleft - counter));
    
     function timeIt(){
       counter ++;
       timer.text(covertSeconds(timeleft - counter));
     };
     setInterval(timeIt, 1000)
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 3
    FYI: setInterval is not accurate – epascarello Oct 08 '20 at 01:59
  • https://stackoverflow.com/questions/19700283/how-to-convert-time-milliseconds-to-hours-min-sec-format-in-javascript/19700358 – epascarello Oct 08 '20 at 02:11
  • You already do the right thing at `sec = s % 60;` but do nothing to that effect for `min` and `hrs`. – Sebastian Simon Oct 08 '20 at 02:41
  • Agree with above, it would be more accurate to set a variable with the timestamp when your timer starts, then use the date object to get a new timestamp in your interval, and examine the difference between start time and current time to get milliseconds elapsed. Then do your math on that product to get it into shape for display. That way, even when setInterval (or timeout) drifts, you aren't actually depending on that having run accurately once a second to get your accurate time elapsed -- it's always based on timestamp math so when setInterval does run, you get accurate measure. – Chris Baker Oct 08 '20 at 02:50

1 Answers1

0

As mentioned in the comments in setInterval is not accurate and you shouldn't use it. However, I think your immediate issue as per the question's title is to do with not updating the seconds variable in convertSeconds, see below for a possible fix:

const pad2 = x => String(x).padStart(2, '0');

const covertSeconds = s => {
  const h = Math.floor(s / 3600);
  s = s % 3600; 
  const m = Math.floor(s / 60);
  s = s % 60;
  return `${pad2(h)}:${pad2(m)}:${pad2(s)}`;
}

let counter = 0;
const timeleft = 36070;
const timer = $("#timer").text(covertSeconds(timeleft - counter));

const timeIt = () => {
  counter++;
  timer.text(covertSeconds(timeleft - counter));
  if (counter == timeleft) {
    clearInterval(intervalID);
  }
};

const intervalID = setInterval(timeIt, 1000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="timer"></div>
Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
  • Please how can I stop the timer from entering negative values and what can I use instead of setInterval. – Nwankwo Fortune Oct 08 '20 at 14:28
  • @NwankwoFortune See the updated code for an example of using [clearInterval](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/clearInterval) to stop the timer from entering negative values. As for not using `setInterval` refer to the this link: https://stackoverflow.com/questions/19700283/how-to-convert-time-milliseconds-to-hours-min-sec-format-in-javascript/19700358 from the comments. – Sash Sinha Oct 08 '20 at 17:39