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

185
Visualizações
Return after clearInterval

I have a code that runs setInterval in a function. What I try to achive is to exit from that function after clearInterval occured. I set up a condition to check wheter the interval is cleared. I'm struggling to find a method to return from the main Start() function based on the current state of the code.

Code:

function Start() {

    var elementX = document.getElementById('moveMeX')
    var elementY = document.getElementById('moveMeY')
    elementY.style.top = "0px"

    var posX = 0
    startPosX = 0
    var posY = 0
    startPosY = 0
    var speed = 1
    var xIsMoving = true
    var yIsMoving = true

    var myIntervalX = setInterval(function() {
        if(startPosX == 0) {
            posX+=speed
            elementX.style.left = posX + 'px'
            if(posX==100) {
                startPosX = 100
            }
        }
        else if (startPosX==100) {
            posX-=speed
            elementX.style.left = posX + 'px'
            if(posX==0) {
                startPosX = 0
            }
        }
    }, 10);
    
    function stopX() {
        clearInterval(myIntervalX);
        xIsMoving = false
    }
    
    elementX.addEventListener("mousedown", stopX);
    elementX.addEventListener("mousedown", startY);
    
    function startY() {
        var myIntervalY = setInterval(function() {
            if(startPosY == 0) {
                posY+=speed 
                elementY.style.top = posY + 'px'
                if(posY==100) {
                    startPosY = 100
                }
            }
            else if (startPosY==100) {
                posY-=speed
                elementY.style.top = posY + 'px'
                if(posY==0) {
                    startPosY = 0
                }
            }
        }, 10);
    
        function stopY() {
            elementY.style.zIndex = "-1";
            clearInterval(myIntervalY);
            yIsMoving = false
        }
        
        elementY.addEventListener("mousedown", stopY);
    }

    if (xIsMoving === false && yIsMoving === false) {
        console.log('stopped')
        stopped = true
    }
}

Start()

Codepen link: https://codepen.io/silentstorm902/pen/podNxVy

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

0

The part you state you have a problem with,

if (xIsMoving === false && yIsMoving === false) {
    console.log('stopped')
    stopped = true
}

currently only runs once: when Start() is first called. At that time, both conditions are false because the variables have their initial declared values of true.

You could create a new setInterval to run this if statement every so often, but it seems unnecessary because your current stopY function is really the place where you know that both xIsMoving and yIsMoving are both false -- because stopY only runs after stopX has run. So just move your statement of stopped=true at the end of the stopY function:

function stopY() {
    elementY.style.zIndex = "-1";
    clearInterval(myIntervalY);
    yIsMoving = false;
    console.log("stopped");
    stopped = true;
}
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