I am adding new features to my already developed application. I am trying to add an auto rendering functionality which automatically render some part of the screen after a certain time interval. I am using setInterval for this, everything is working fine.
My question is, how should I call the clearInterval(intervalId) function for all other events except one. Please consider an example:
Suppose I have some events related to current screen, and I want to start my setInterval only on “.myBtn” click and stop for all other events.
var intervalId = 0;
$(".myBtn").click(function(){
intervalId = setInterval(function(){
alert("execute something");
}, 2000);
});
$(".otherElm").click(function(){
// some other stuff.
});
$(".otherElm1").click(function(){
// some other stuff.
});
$(".otherElm2").click(function(){
// some other stuff.
});
For the above situation, I know only one way to stop the interval i.e. to put “clearInterval(intervalId)” in every other event.
But, is there any other way to do this?
Hope I have put everything so that one can understand my question.
Thanks in advance
You are right in your comment – you have to put clearInterval(IntervalId) into all of the events that you require to stop the event. Is there a problem with doing this?
Edit:
Identify them all with a class – say elm. Once this is done, use $(“.elm”).click(clearinterval).