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

138
Views
How to add start button using javascript

I'm trying to create countdown timer. Everytime i reload the page, the timer start directly.. I want to add start button to let the time start after i press the button.

Below is my code:

var counter = 30;
var mode = "Workout";
var rounds = 1;

var interval = setInterval(function() {
  counter--;

  if (mode == "Workout" && counter >= 0) {
    id = document.getElementById("count");
    id.innerHTML = "ROUND " + rounds + " <br> " + counter;
  } else {
    id = document.getElementById("count");
    id.innerHTML = "REST " + " <br> " + counter;
  }

  if (counter === 0) {
    if (mode == "Workout") {
      mode = "Rest";
    } else {
      mode = "Workout";
      rounds++;
    }
    id.innerHTML = "FINISHED";
    counter = 30;
    if (rounds == 21) {
      clearInterval(interval);
    }
  }

}, 1000);
<div>
  <h1 id="count"></h1>
</div>

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

0

You can wrap your whole code in to a function:

const startTimer = () => {
    var counter = 30;
    var mode = "Workout";
    var rounds = 1;
    
    var interval = setInterval( function(){
      counter--;
    
      if(mode == "Workout" && counter >= 0 ){
        id = document.getElementById("count");
        id.innerHTML = "ROUND " + rounds + " <br> " + counter;
      }
      else{
        id = document.getElementById("count");
        id.innerHTML = "REST "+ " <br> " + counter;
      }
    
      if( counter === 0 ){
        if(mode == "Workout"){
           mode = "Rest";
        }
        else{
           mode = "Workout";
           rounds++;
        }
        id.innerHTML = "FINISHED";
        counter = 30;
        if (rounds == 21){
          clearInterval(interval);
        }
      }

    }, 1000);
}

and trigger it via event Event Listener

add button:

<button id="btn">Start Timer</button>

add listener:

document.querySelector('#btn').addEventListener('click', startTimer);
about 4 years ago · Juan Pablo Isaza Report

0

Just wrap your code in a function, and add that to the button as an event listener listening for click events:

document.getElementById('start').addEventListener('click', startCountdown);

let interval;

function startCountdown() {
  let counter = 30,
    mode = "Workout",
    rounds = 1;
  // if a countdown is already running, stop it
  if (interval) clearInterval(interval);
  
  interval = setInterval(function() {
    counter--;

    if (mode == "Workout" && counter >= 0) {
      id = document.getElementById("count");
      id.innerHTML = "ROUND " + rounds + " <br> " + counter;
    } else {
      id = document.getElementById("count");
      id.innerHTML = "REST " + " <br> " + counter;
    }

    if (counter === 0) {
      if (mode == "Workout") {
        mode = "Rest";
      } else {
        mode = "Workout";
        rounds++;
      }
      id.innerHTML = "FINISHED";
      counter = 30;
      if (rounds == 21) {
        clearInterval(interval);
      }
    }

  }, 1000);
}
<div>
  <h1 id="count"></h1>
</div>
<button type="button" id="start">Start Countdown</button>

about 4 years ago · Juan Pablo Isaza Report

0

You can wrap your code with a start function which you call on a button click event:

var counter = 30;
var mode = "Workout";
var rounds = 1;

document.getElementById('startbutton').addEventListener('click', startTimer)

function startTimer(){
var interval = setInterval(function() {
  counter--;

  if (mode == "Workout" && counter >= 0) {
    id = document.getElementById("count");
    id.innerHTML = "ROUND " + rounds + " <br> " + counter;
  } else {
    id = document.getElementById("count");
    id.innerHTML = "REST " + " <br> " + counter;
  }

  if (counter === 0) {
    if (mode == "Workout") {
      mode = "Rest";
    } else {
      mode = "Workout";
      rounds++;
    }
    id.innerHTML = "FINISHED";
    counter = 30;
    if (rounds == 21) {
      clearInterval(interval);
    }
  }

}, 1000);
}
<div>
  <h1 id="count"></h1>
  <button id='startbutton'>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!