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

298
Vistas
Reiniciar el temporizador cada vez que se vuelve a hacer clic en el botón

Tengo un botón cuando se hace clic inicia un temporizador. El problema es que cuando se vuelve a hacer clic en el botón, el temporizador no se reinicia, simplemente se vuelve loco rápido.

 var timeleft = 1800; function rig() { var Timer = setInterval(function function1(){ document.getElementById("countdown").innerHTML = Math.floor(timeleft / 60) + ":" + (timeleft % 60 ? timeleft % 60 : '00') + "&nbsp"+"minutes remaining"; timeleft -= 1; if(timeleft <= 0){ clearInterval(Timer); document.getElementById("countdown").innerHTML = "Time out" } }, 1000); console.log(countdown) } <p id="countdown"></p> <button onclick="rig();">Start Time</button>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Use una variable que establezca cada vez que el temporizador se inicie y se detenga.

pausa el tiempo

 let timeBreak = false; var timeleft = 1800; function rig() { if (!timeBreak) { timer = setInterval(function function1() { document.getElementById("countdown").innerHTML = Math.floor(timeleft / 60) + ":" + (timeleft % 60 ? timeleft % 60 : '00') + "&nbsp" + "minutes remaining"; timeleft -= 1; if (timeleft <= 0) { clearInterval(timer); document.getElementById("countdown").innerHTML = "Time out" } }, 1000); timeBreak = true; } else { clearInterval(timer); timeBreak = false; } }
 <p id="countdown"></p> <button onclick="rig();">Start Time</button>

restablecer el tiempo

 let timeBreak = false; var timeleft = 1800; function rig() { if (!timeBreak) { timer = setInterval(function function1() { document.getElementById("countdown").innerHTML = Math.floor(timeleft / 60) + ":" + (timeleft % 60 ? timeleft % 60 : '00') + "&nbsp" + "minutes remaining"; timeleft -= 1; if (timeleft <= 0) { clearInterval(timer); document.getElementById("countdown").innerHTML = "Time out" } }, 1000); timeBreak = true; } else { document.getElementById("countdown").innerHTML = ''; // Remove that line if you want to see the stopped time clearInterval(timer); timeleft = 1800; timeBreak = false; } }
 <p id="countdown"></p> <button onclick="rig();">Start Time</button>

También asegúrese de no usar var , let o const antes de timer , ya que desea usar esa variable fuera del alcance.

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