R – How does one break out of a Set Interval in Action Script 2

flashintervals

p2 = setInterval(function ()
{
clearInterval(p2);
some code here;
}, waitTime)

I need to break out of this interval in a separate function.

This plays an array of SWF movies – there is a purge function that needs to stop this interval.

How can I stop this interval from a separate function in AS2?

Best Solution

the whole thing with setInterval is scope.

the way your code looks, p2 is in the main timeline and it belongs to this/_level0/_root right ?

clearInterval() is a global function as well, so you can call it from any other function nested in any movie clip, as long as you can get access to the interval's id (p2 in your case)

so if you have some like:

p2 = setInterval(function () { trace('p2 running'); }, waitTime);

you can have a separate function like

function clearP2(){
clearInterval(p2);
}

if that function is nested in some clip you can always use the dirty all _root ( as in absolute path )

e.g. //clearP2 lives in a nested clip far far away from _root

function clearP2(){
clearInterval(_root.p2);
}

of course you can use relative paths as well

function clearP2(){
clearInterval(_parent._parent._parent.p2);//depending on the clips hierarchy
}

the idea to keep in mind is to make sure you can access the interval's ID where you need it, clearInterval() is global

Related Question