How to stop running function setinterval when I call another?

Asked

Viewed 16 times

-2

        let main = document.getElementById("main");
        let cont = 10
        let wid = 0
        var tam = document.querySelector("h1");
        function aumenta(){
            wid = wid +cont
            main.style.width=`${wid}px`
            if(wid>1360){
                wid =1360
            }
            tam.innerHTML=wid
        }
        function diminui(){
            wid = wid - cont
            main.style.width=`${wid}px`
            if(wid<10){
                wid=0
            }
            tam.innerHTML=wid
        }

        function aum(){
            setInterval(aumenta,200)

        }
        function dim(){
            setInterval(diminui,200)
        }

1 answer

2


Using clearInterval, thus:

var intervalAum, intervalDim;

function aum(){
    clearInterval(intervalDim); //Para as repetições de "dim"

    intervalAum = setInterval(aumenta,200)

}

function dim(){
    clearInterval(intervalAum); //Para as repetições de "aum"

    intervalDim = setInterval(diminui,200)
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.