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

110
Vistas
How to clear interval with a btn if it's already set

So I'm currently very new to JavaScript and it's concepts and am trying to make a game where there are 4 btns which start a timer, then reset it and start it again when either of them are pressed the second time, but whenever I press any button twice it speeds up the timer 2x instead of resetting and starting it again

This is my code, apologies if it's is messy and/or hard to understand

let time = 5
const countDown = document.querySelector('#countdown')
let challenge = document.getElementById('text')
const btns = document.querySelectorAll('.btn')
let text = ''

function randomText(){
  return Math.floor(Math.random() * arr.length)
}

btns.forEach(function(btn){
  btn.addEventListener('click', function(e){
    time = 5
    const timer = setInterval(countdown, 1000)
    text = arr[randomText()]
    challenge.textContent = text
  })
})



function countdown(){
  if( time === 0 ){
    time = 5
}
  time--
  countDown.innerHTML = `00:0${time}`
} 
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

let timer;
btns.forEach(function(btn){
  btn.addEventListener('click', function(e){
    time = 5
    clearInterval(timer);
    timer = setInterval(countdown, 1000)
    text = arr[randomText()]
    challenge.textContent = text
  })
})
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