Copy link to clipboard
Copied
If I create an addEventListener for a variable that I create inside a function, do I need to worry about removing that addEventListener when I come out of that function?
Say I create a function shows job is just to caculate some math, and inside that function I create a variable and create a addEventListener for that variable, and then after solving the math, the program comes out of the function and doesn't use the function until another time in the future. Does that addEventListener still exist in the program, or does Animate CC automatically clean up that addEventListener for me and I do not have to worry about using the removeEventListener function?
Thanks
Are you working with an AS3 or Canvas project?
Either way, you can't remove a listener with an anonymous event handler, because removeEventListener() requires passing in exactly the same handler function that was used with addEventListener().
flash - remove a listener that has an anonymous function in Actionscript 3 - Stack Overflow
Copy link to clipboard
Copied
There's no such thing as an event listener for a variable. Event listeners are for objects. What "event" from the variable do you intend to be listening for?
Anyway, if you use addEventListener(), yes, it will fire every time its event happens, until your explicitly remove the listener with removeEventListener(). However, if you use on(), you can set a flag which will make it only fire once.
Copy link to clipboard
Copied
Yes sorry, I meant to say object instead of variable. Here is the example for what I mean:
function mathCal ():void {
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, function (error:Event): void {trace(error)});
urlLoader.load(request);
urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, function (error:Event): void {trace(error)});
}
In the example above is it necessary for me to use the removeEventListener, or will the addEventListener I made automatically get removed when we come out of the function?
Copy link to clipboard
Copied
Are you working with an AS3 or Canvas project?
Either way, you can't remove a listener with an anonymous event handler, because removeEventListener() requires passing in exactly the same handler function that was used with addEventListener().
flash - remove a listener that has an anonymous function in Actionscript 3 - Stack Overflow
Copy link to clipboard
Copied
Working with AS3. So far the code is setup similar to the way I have it above and I'm getting no errors or anything. I just wanted to verify if I write it in such a way if I needed to use removeEventListener() at all since I used the addEventListener within a function. Usually I know for example if I make variables in a function, they are only usable within that function, but with the addEventListener() I wasn't sure how similar it is.
Copy link to clipboard
Copied
I'm not AS3 expert, but in JavaScript functions hang around as long as anything is referencing them, even after their containing code has exited (this is called a closure). So there's a good chance your event handler is still live.
ActionScript 3.0 using closures for event handlers - Stack Overflow