Skip to main content
Participant
May 2, 2022
Question

Can't stop setInterval with clearInterval

  • May 2, 2022
  • 1 reply
  • 1660 views
var mySeconds = 5;
this.seconds_txt.text = mySeconds;
var timerId = setInterval(countDown.bind(this), 1000);

function countDown(){
if(mySeconds == 0){

   this.myStopIntervalFunction(); //call to function fires but doesn't clear interval
	clearInterval(countDown); //doesn't work

}
else{
     mySeconds--;
     this.seconds_txt.text = mySeconds;
     }
}

this.myStopIntervalFunction = function (root) {
	console.log("myStopFunction fired");
	root.clearInterval(countDown);
};

 

I have no idea why its not working. 

    This topic has been closed for replies.

    1 reply

    JoãoCésar17023019
    Community Expert
    Community Expert
    May 2, 2022

    Hi.

     

    You have to pass the interval ID not the function. Like this:

    clearInterval(timerId);

     

    More info:

    https://www.w3schools.com/jsref/met_win_setinterval.asp

     

    Regards,

    JC