Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda