Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

313
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda