Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
Countdown does not stop properly

Onclick Countdown for an Alarmclock does not stop properly. It stops at "-1:0" and not 0:00 as planed. Tryed to solve the Problem by changing the "> < =" Operators but i dont get it right. Maybe someone can help me with that.

function startTimer() {
  let startTime = new Date().getTime();
  let fiveMinutes = 5 * 1 * 1000;
  let endTime = startTime + fiveMinutes;

  var countdown = setInterval(function count() {
    let timeLeft = endTime - new Date().getTime();
    let minutes = timeLeft / (1000 * 60);
    minutes = Math.floor(minutes);
    let seconds = (timeLeft / 1000) % 60;
    seconds = Math.round(seconds);
    let text = minutes + ':' + seconds;
    timer.innerHTML = text;

    if ((minutes <= 0) && (seconds <= 0)) {
      clearInterval(countdown);
    }
  }, 1000);
}
<div class="timer center margin-top" id="timer">
  00:05
</div>

<div class="button center">
  <img onclick="startTimer()" img src="img/btn.png" />
</div>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Your problem is only using Math.floor(minutes). try to use Math.round(minutes)

this is code for solve your problem :

function startTimer() {
  let startTime = new Date().getTime();
  let fiveMinutes = 5 * 1 * 1000;
  let endTime = startTime + fiveMinutes;

  var countdown = setInterval(function () {
    let timeLeft = endTime - new Date().getTime();
    let minutes = timeLeft / (1000 * 60);
    minutes = Math.round(minutes);
    let seconds = (timeLeft / 1000) % 60;
    seconds = Math.round(seconds);
    let text = minutes + ':' + seconds;
    timer.innerHTML = text;

    if ((minutes <= 0) && (seconds <= 0)) {
      clearInterval(countdown);
    }
  }, 1000);
}
<div class="timer center margin-top" id="timer">
  00:05
</div>

<div class="button center">
  <img onclick="startTimer()" img src="img/btn.png" alt="button start" />
</div>

about 4 years ago · Juan Pablo Isaza Report

0

function startTimer() {
  let startTime = new Date().getTime();
  // If its five minute  5 * 60 * 1000;
  let fiveSeconds = 5 * 1000;
  let endTime = startTime + fiveSeconds;

  var countdown = setInterval(function count() {
    let timeLeft = endTime - new Date().getTime();
    let minutes = Math.floor((timeLeft / (1000 * 60)) % 60);
    
    let seconds = Math.floor((timeLeft / 1000) % 60);
    
    // Once test is passed do the DOM manipulation
    let text = minutes + ':' + seconds;
    timer.innerHTML = text;
    
    if ((minutes <= 0) && (seconds === 0)) {
      return clearInterval(countdown);
    }
  }, 1000);
}
<div class="timer center margin-top" id="timer">
  5:00
</div>

<div class="button center">
  <button onclick="startTimer()">Start</button>
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!