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

208
Vistas
How can I get my "clickMe!" button to work after I clear? All I get is "time's up!"

What I would like to happen after I use my Clear button, is to have my timer restart when I click the Click Me button another time. No matter what I try it will not work.

var sec = 5;
var timerID;

document.getElementById("b1").onclick = function() {
  timerID = setInterval(myTimer, 1000);
};

function myTimer() {
  document.getElementById("b1").onclick = function() {
    myTimer()
  }

  document.getElementById('demo').innerHTML = sec + "sec.";
  sec--;
  if (sec <= -1) {
    clearInterval(timerID);
    document.getElementById("demo").innerHTML = "Time's Up!";
  }
}

$(document).ready(function() {
  $("#b2").click(function() {
    $("#demo").html("");
  })
})
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>

<body>
  <button id="b1">Click Me!</button>
  <p id="demo"></p>
  <button id="b2">Clear!</button>
</body>

</html>

What I would like my "Click Me" button to do the second time is to restart the timer. Thatis not what is happening. I ave tried many things all to no avail.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Check This Out! :)

var sec = 5;
var timerID;

document.getElementById("b1").onclick = function() {
  timerID = setInterval(myTimer, 1000);
};

function myTimer() {
//   document.getElementById("b1").onclick = function() {
//     myTimer()
//   }
  document.getElementById('demo').innerHTML = sec + "sec.";
  sec--;
  if (sec <= -1) {
    clearInterval(timerID);
    document.getElementById("demo").innerHTML = "Time's Up!";
  }
}

$(document).ready(function() {
  $("#b2").click(function() {
    clearInterval(timerID);
    sec = 5;
    $("#demo").html("");
  })
})
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>

<body>
  <button id="b1">Click Me!</button>
  <p id="demo"></p>
  <button id="b2">Clear!</button>
</body>

</html>

about 4 years ago · Juan Pablo Isaza Denunciar

0

In addition to removing the element's innerHTML, you also need to clear the interval.

var sec = 5;
var timerID;
document.getElementById("b1").onclick = function() {
  timerID = setInterval(myTimer, 1000);
};

function myTimer() {
  document.getElementById("b1").onclick = function() {
    myTimer()
  }
  document.getElementById('demo').innerHTML = sec + "sec.";
  sec--;
  if (sec <= -1) {
    clearInterval(timerID);
    document.getElementById("demo").innerHTML = "Time's Up!";
  }
}

$(document).ready(function() {
  $("#b2").click(function() {
    $("#demo").html("");
    clearInterval(timerID);
  })
})
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>

<body>
  <button id="b1">Click Me!</button>
  <p id="demo"></p>
  <button id="b2">Clear!</button>
</body>

</html>

about 4 years ago · Juan Pablo Isaza Denunciar

0

The myTimer() function is removing the onclick action that starts the timer.

Just have the Clear button stop the timer, and keep the original onclick.

var sec = 5;
var timerID;

document.getElementById("b1").onclick = function() {
  timerID = setInterval(myTimer, 1000);
};

function myTimer() {
  document.getElementById('demo').innerHTML = sec + "sec.";
  sec--;
  if (sec <= -1) {
    clearInterval(timerID);
    document.getElementById("demo").innerHTML = "Time's Up!";
  }
}

$(document).ready(function() {
  $("#b2").click(function() {
    $("#demo").html("");
    clearInterval(timerID);
  })
})
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>

<body>
  <button id="b1">Click Me!</button>
  <p id="demo"></p>
  <button id="b2">Clear!</button>
</body>

</html>

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