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

180
Vistas
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 Respuestas
Responde la pregunta

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 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