オブジェクトインスタンスを適切に「クリア」する方法を知りたいです。以下のコードでは、インスタンスが親によって「クリア」された後でも、内部の setInterval() が引き続き実行されます。
// simple Class
var aClass = function(){
return {
init: function(){
console.log("aClass init()")
setInterval( this.tick, 1000 );
// note: we're not storing the ref
},
tick: function(){
console.log("aClass tick");
}
}
}
// instantiate the class
var inst = new aClass();
inst.init();
// try to forget the instance
function test(){
console.log("test() 1 inst:", inst);
inst = null;
console.log("test() 2 inst:", inst);
}
// run for a while, then call test()
setTimeout( test, 4000 );
出力:
aClass init()
aClass tick
aClass tick
aClass tick
test() 1 inst: {.....}
test() 2 inst: null
aClass tick
aClass tick ...
問題は、「aClass tick」メッセージが test() の後に出力され続けることです。
アイデア?