Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Can't stop setInterval with clearInterval

Community Beginner ,
May 02, 2022 May 02, 2022
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. 

1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 02, 2022 May 02, 2022
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines