Make your own free website on Tripod.com

The scope of function in setInterval
by Eric Lin


Question: I load an external movie where I  "setInterval" to execute a function defined in the external movie. What will happed if I unload that external movie without clear Interval. Will that function continue to be executed ?

Here is a movie: SetInterval is defined on movie1 to move the square mc. Then I load a new movie to replace it. The right side button create a "target" for setInterval, and the target successfully moves.

download zip


In fact, what I am going to discuss is not specifically to setInterval. It is about the scope of function. Any function. It is just that, the rules also apply to setInterval and problems occurrs more often if we use setInterval.

Where is the body of the object and function put in Flash system ?

Let's try the first code. In the movieClip mc1, I write the codes below.

array_mc=[0,1,2];
_root.array_r=array_mc;
delete array_mc;
trace(_root.array_r);

Well, this array is still there. What we deleted is just the "reference" of that array. This is the first thing we should know.

The second thing we should know is that, when we say _root.array_r=array_mc, --> _root.array_r stores direct reference to that array itself. It does not store the reference of "mc.array_mc"; When we want to know what is _root.array_r, it needs not consult mc.array_mc to know what is that array. In other word, if we change the reference of mc.array_mc to another array, it will not affect _root.array_r; So, it is easy to understand why deleting array_mc will not affect _root.array_r.

The third thing we should know is: This array exists in the Flash system. Only the reference exists in each timeline. When mc is unloaded, removed or destroyed, only the reference is deleted, not the array itself. In this case, we can not access the array by mc.array_mc anymore, because Flash does not know what is mc.array_mc. But, we can access that array by _root.array_r, because _root.array_r still stores the reference to that array.

The fourth thing is about manipulation of the array. When we say, mc.array_mc=[]; It wont change the original array; The _root.array_r remains the same. However, if we say mc.array_mc.push(3); then the original array does change into [0,1,2,3]; Trace(_root.array_r) will show the change. So, we know the difference between changing the reference and changing the array itself.

The difference between reference and body itself exists for all object and also functions.

Now lets try another code: in a movieClip of an exteranl movie, we write the codes below:

move=function(){
    _x+=5;
}
setInterval(move,500);

setInterval is a global function, so we can not use "this" in that function. This function is defined from mc of _level2 movie, so the functioni in fact is :
_level2.mc._x+=5; We see a movieClip in _level2 moving horizontally.

Now, we unload _level2 without clear interval. We know that, unload movie will just remove the variable:"move", and the function body is not affected. setInterval will continue to execute the function codes without accessing the variable "move".

Since there is no _level2.mc, it pass the reference null to "_flash". The result is a horizontal move of the whole movie;

What if we load a new external movie to _level2 ? If there is no a movieClip named "mc", then the target is still null, it is the whole movie that moves.

If there is a movieClip named "mc" in _level2, then the target will be successful and that movieClip will be moved. Rename a movieClip._name will get the same success. 

Here is a tricky point: 

In our function, we just said "_x+=5"; Flash add an internal object to it like this {_level2.mc}._x+=5; We did not really say "_level2.mc._x+=5;". If we hardcodes the path into the function body , will there be any difference between these two codeing ? Yes, there is difference. Flash will not search for that internal movieclip object, but search for what "_level2.mc" is. We need not really provide an movieClip with _name="mc" for fit of internal object. We can just provide a variable "mc" for it. So, we said, _level2.mc=_level1.cow; then _level1.cow will be moved by setInterval.

What is the significance of this article ?

Why ? I have seen some using setInterval to do animation instead of onEnterFrame. The pitfall is that, once we create setInterval, it must be removed by clear interval. If this movie is load to other movie, it will stay there rayally do its job for specific interval. It will not be cleared automatically by unload movie.