views:

68

answers:

2

I'd be happy if you could explain to me why shout() keeps getting called, although it's supposedly "gone".

var myclass = new Class({
myid: "greatidea",
initialize: function(element) {
    var shout = function() { alert(this.myid); };
    shout.periodical(5000, this); // test debug
}

});
x = new myclass ();
alert(x);
x=null;
alert(x);

also see here http://mootools.net/shell/jhCBz/

Basically, I get the idea: the function gets its own registration, and is now independent of the object who called it. But I'd be happy to get a real explanation.
Thanks.

A: 

x held a reference to myclass. myclass is executing shout. When you set x to null, you are just getting rid of your reference to myclass, not the myclass object itself.

jball
Thanks a lot, @jball. Is there another way to stop the function calls (except for clearing the periodical timer)?
Nir
No, a call to '$clear' is the only way I know about.
jball
yeah it fails even if the periodical reference is a property of the class: http://mootools.net/shell/ZBrm7/1/ it continues even if you remove the class itself. very annoying - but you may have to code a .destroy() method which does $clear(this.timer);
Dimitar Christoff
Thanks a lot, @Dimitar!
Nir
I built a different periodical/delay interface which allows you to track and remove any deferred functions at your whim - check http://mootools.net/shell/judGJ/ - it may give you some ideas and you can build that into your class. Even though it's fairly robust/fast and serves my garbage collection purposes, i would not recommend using it in animations that are FPS dependent due to overhead of moving data.
Dimitar Christoff
+5  A: 

Functions are always independent. As long as there's a reference to a function, it continues to exist. And in this case, so does your object, since you've passed a reference to it (via this) to the periodical() function, which holds onto it for context.

Shog9