Clearing+Intervals+and+Listeners

Return to Code Snippets

=Clearing Intervals and Listeners=

Problem:
It turns out that there are some objects in Flash that can exist with or without the movieclip that defined its existence ... and can virtually survive on the ether inside a SWF. Such nebulous and potentially troublesome beasties include setTimeout function calls, setIntervals, Key and Mouse listeners, and potentially other types of listeners as well.

In all of the above cases, a function or set of functions is defined that will react to something internal (like a set period of time passing) or something external (like a key or mouse being pressed). The interval or listener is assigned a value (a handle) by which it can then be referenced, and eventually disposed of. HOWEVER ... if the movie clip in which that variable is created, it is no longer possible to control (or dispose) of the interval or listener - BUT - the functions defined remain in memory and will continue to be called and performed, according to how they were established (every n milliseconds / every mouse down / key click, etc..)

Solution:
For each scene, create an array in which to store all possible intervals and listeners (or a set of arrays as shown below). Define an unLoad function that will traverse these arrays and clear all found intervals and listeners. Throughout the scene, as intervals and listeners are created in the actions layer, follow each creation command with a "registration" line, pushing the new intervalID or listenerID into the appropriate array.

Example:
code format="actionscript" var TimeOutsArray:Array = new Array; var SetIntervalsArray:Array = new Array; var KeyListenersArray:Array = new Array; var MouseListenersArray:Array = new Array;
 * FRAME 1 - globals & sound layer :**

this.onUnload = function { trace("\nUNLOADING ...\n"); var tmpTOuts = TimeOutsArray.length; var tmpSetIs = SetIntervalsArray.length; var tmpKeyLs = KeyListenersArray.length; var tmpMouseLs = KeyListenersArray.length;

for (var i = 0; i<tmpTOuts; i++) { clearTimeout(TimeOutsArray[i]); trace("clearing timeout "+i); } for (var j = 0; j<tmpSetIs; j++) { clearInterval(SetIntervalsArray[j]); trace("clearing interval "+j); } for (var k = 0; k<tmpKeyLs; k++) { Key.removeListener(KeyListenersArray[k]); trace("clearing key listener "+k); } for (var m = 0; m<tmpMouseLs; m++) { Mouse.removeListener(MouseListenersArray[m]); trace("clearing mouse listener "+m); } }; code code format="actionscript" var timeoutID = setTimeout(delay, 1000, this._parent._currentframe, this._currentframe ); //delay before entering next frame TimeOutsArray.push( timeoutID );
 * FRAME 2 - actions layer :**

function delay(sc, fr) { var thisSc = _parent._currentframe; var thisFr = _currentframe; trace("timeoutID = "+timeoutID+" ("+sc+","+fr+") - playing on Scene "+thisSc+" Frame "+thisFr); } code code format="actionscript" var intervalID = setInterval ( doSomething, 1000 , this._parent._currentframe, this._currentframe ); //delay before entering next frame SetIntervalsArray.push ( intervalID );
 * FRAME 3 - actions layer :**

function doSomething(sc, fr) { var thisSc = _parent._currentframe; var thisFr = _currentframe; trace(" intervalID = "+this.intervalID+" ("+sc+","+fr+") - playing on Scene "+thisSc+" Frame "+thisFr); } code code format="actionscript" var myListener:Object = new Object; myListener.onKeyDown = function { trace("You pressed a key."); }; myListener.onKeyUp = function { trace("You released a key."); }; Key.addListener(myListener); KeyListenersArray.push(myListener); code code format="actionscript" var mouseListener:Object = new Object; mouseListener.onMouseDown = function { trace ("mouse moved to " + _xmouse + "," + _ymouse ); }; Mouse.addListener(mouseListener); MouseListenersArray.push(mouseListener); code
 * FRAME 4 - actions layer :**
 * FRAME 5 - actions layer :**