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

308
Views
How do you stop or disable a button after I have clicked it (setInterval)?

I made a count down timer and the start button starts the count with a setInterval. My problem is that if a user accidently clicks on the start button again, it starts another interval and speeds up the count down.

const timeH = document.querySelector("h1");

let timeSecond = prompt("Enter minutes here") * 60;

displayTime(timeSecond);

function displayTime(second) {
  const min = Math.floor(second / 60);
  const sec = Math.floor(second % 60);
  timeH.innerHTML = `${min < 10 ? "0" : ""}${min}:${sec < 10 ? "0" : ""}${sec}`;
}

function endTime() {
  timeH.innerHTML = "TIME OUT";
}

function countDown() {
  timeSecond--;
  displayTime(timeSecond);

  if (timeSecond <= 0 || timeSecond < 1) {
    endTime();
  }
}

document.querySelector(".start").addEventListener("click", () => {
  count = setInterval(countDown, 1000);
});

document.querySelector(".stop").addEventListener("click", () => {
  clearInterval(count);
});

document.querySelector(".reset").addEventListener("click", () => {
  clearInterval(count);
  timeSecond = prompt("Enter minutes here") * 60;

  displayTime(timeSecond);
});
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Change

document.querySelector(".start").addEventListener("click", () => {
  count = setInterval(countDown, 1000);
});

document.querySelector(".stop").addEventListener("click", () => {
  clearInterval(count);
});

document.querySelector(".reset").addEventListener("click", () => {
  clearInterval(count);
  timeSecond = prompt("Enter minutes here") * 60;

  displayTime(timeSecond);
});

to

let startButton = document.querySelector(".start");
let stopButton = document.querySelector(".stop");

startButton.addEventListener("click", () => {
  count = setInterval(countDown, 1000);
  
  startButton.disabled = true;
});

stopButton.addEventListener("click", () => {
  clearInterval(count);

  startButton.disabled = false;
});

document.querySelector(".reset").addEventListener("click", () => {
  clearInterval(count);
  startButton.disabled = false;

  timeSecond = prompt("Enter minutes here") * 60;

  displayTime(timeSecond);
});

The disabled property disables the button when it is set to true. You can read more on that property on MDN: https://developer.mozilla.org/en-US/docs/Web/API/HTMLButtonElement/disabled

about 4 years ago · Juan Pablo Isaza Report

0

So add a check that it is running

document.querySelector(".start").addEventListener("click", () => {
  if (count) return;
  count = setInterval(countDown, 1000);
});

function stopTimer() {
  if (!count) return;
  clearInterval(count);
  count = null;
}

document.querySelector(".stop").addEventListener("click", stopTimer);

document.querySelector(".reset").addEventListener("click", () => {
  stopTimer();
  timeSecond = prompt("Enter minutes here") * 60;
  displayTime(timeSecond);
});
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!