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

118
Visualizações
Programming a stop timer Button

So I created a website that's going to be a game eventually for a personal project and I've been working on my start and stop button for a countdown timer. My Start button works and counts down perfectly but my stop button doesn't stop my countdown timer like it's supposed to.

This is my javaScript functions for the start and stop buttons for the timer

function countDown(){
var currTime = 55
var timedown = setInterval(function() {
    currTime = currTime - 1;
    document.getElementById("countdowntimer").innerHTML ="The time is:" + currTime; 
        if(currTime == 0){
            clearInterval(timedown);
            document.getElementById("countdowntimer").innerHTML = "BLASTOFF!"
        }
        else if(currTime < 25){
            document.getElementById("countdowntimer").innerHTML = "Half way to launch " + currTime;

        }    //this is my count down timer function

        




}, 1000);}

function stopCount() {
   
    for (var i = 0; i < countDown.length; i++) {
        clearInterval(countDown[i]);        
    }

}

 //this is supposed to be my stop count function that isn't working 

and then this is the part of the HTML file where im calling upon them. My start button works but my stop doesn't

<button id="stopTime" onclick = stopCount() >Stop!</button>//stop Timer button
<button onclick=countDown()>Start Countdown!</button> //Start timer button

I don't know what im doing wrong here but i just can't get it to work

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The interval isn't scoped to a place where stopCount() can access it. This is a working example. I've shortened the time for testing.

var timedown
function countDown(){
var currTime = 4
timedown = setInterval(function() {
    currTime = currTime - 1;
    document.getElementById("countdowntimer").innerHTML ="The time is:" + currTime; 
        if(currTime == 0){
            clearInterval(timedown);
            document.getElementById("countdowntimer").innerHTML = "BLASTOFF!"
        }
        else if(currTime <= 2){
            document.getElementById("countdowntimer").innerHTML = "Half way to launch " + currTime;

        } 
}, 1000);}

function stopCount() {
        clearInterval(timedown)
    }
<button id="stopTime" onclick ='stopCount()' >Stop!</button>
<button onclick='countDown()'>Start Countdown!</button>
<div id='countdowntimer'>timedown is now scoped globally and is accessable by the stopCount function</div>

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