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

111
Views
Can't get my timer to start when I click on my buttons

I am trying to get my buttons to start counting down when I select a specific time. As of now when I click on one it only shows the time, it doesn't start the countdown.

 <html>
     <div class="app">
        <div class="time-select">
        <button data-time="120">2 Minutes</button>
        <button data-time="300">5 Minutes</button>
        <button data-time="600">10 Minutes</button>
    </div>

    <div class="timer-container">
            <h3 class="time-display">0:00</h3>
        </div>
  </div>
 </html>


     const timeDisplay = document.querySelector(".time-display");

     const timeSelect = document.querySelectorAll(".time-select button");


     let fakeDuration = 600;
 
      timeSelect.forEach(option => {    
      option.addEventListener("click", function(){    
      fakeDuration = this.getAttribute("data-time");    
      timeDisplay.textContent = `${Math.floor(fakeDuration / 60)}:${Math.floor(fakeDuration % 
      60)}`;
      });
    });


      ontimeUpdate = () => {
        let elapsed = fakeDuration;
        let seconds = Math.floor(elapsed % 60);
        let minutes = Math.floor(elapsed / 60);

    timeDisplay.textContent = `${minutes}:${seconds}`;
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to use setInterval() - additionally I fixed the way you calculate minutes and seconds. JS uses milliseconds - it's generally best to standardize time-keeping/counting to fit into JS's existing rules.

const timeDisplay = document.querySelector(".time-display");
const timeSelect = document.querySelectorAll(".time-select button");
let fakeDuration, interval

timeSelect.forEach(option => {
  option.addEventListener("click", function() {
    clearInterval(interval)
    fakeDuration = +this.getAttribute("data-time");
      interval = setInterval( () =>  ontimeUpdate(), 1000)
  });
});


ontimeUpdate = () => {
  fakeDuration -= 1000;
  if (fakeDuration <= 0) clearInterval(interval)
  let minutes = ('0' + Math.floor(fakeDuration / 60000)).slice(-2);
  let seconds = ('0' + (fakeDuration % 60000)/1000).slice(-2);
  timeDisplay.textContent = `${minutes}:${seconds}`;
};
<div class="app">
  <div class="time-select">
    <button data-time="120000">2 Minutes</button>
    <button data-time="300000">5 Minutes</button>
    <button data-time="600000">10 Minutes</button>
  </div>

  <div class="timer-container">
    <h3 class="time-display">0:00</h3>
  </div>
</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!