PDA

View Full Version : setInterval scope


emsee
08-26-2004, 04:44 PM
Hello!

I'm trying to get to grips with as2, but I'm having problems with setInterval. Anyone know what I'm doing wrong?

//Class
class Test {

private var intervalID:Number;

function Test() {
loadFile();
}

private function loadFile():Void {
intervalID = setInterval(this, "readTrack", 1000, "foo");
}

private function readTrack(sString:String):Void {
trace(sString);
var myThing:String = "bar";
displayThing(myThing);
}

private function displayThing(vThing:String):Void {
trace(vThing);
}

}


//Code in movie
stop();
function init() {
var thing:Test = new Test();
}
init();

Any help you can give me is appreciated - thanks!

senocular
08-26-2004, 05:01 PM
dont make your class instance a local (i.e. temporary) variable.

stop();
var thing:Test;
function init() {
thing = new Test();
}
init();

deQue
08-26-2004, 05:13 PM
You have an error in your interval deffinision:
intervalID = setInterval(this, "readTrack", 1000, "foo");
Should be:
intervalID = setInterval(readTrack, 1000, "foo");

emsee
08-26-2004, 05:16 PM
var emsee:Dumbness;
function init() {
emsee = new Dumbness();
}
init()


Thanks senocular, I think I must be going blind!

emsee
08-26-2004, 05:19 PM
deQue: actually, that's to provide the readTrack function with scope. Otherwise it can't call the displayThing method.

senocular
08-26-2004, 08:35 PM
var emsee:Dumbness;
function init() {
emsee = new Dumbness();
}
init()


Thanks senocular, I think I must be going blind!


Its actually a very common problem. Whether you meant to make it a local variable or not, its not too intuitive that an interval in such a situation will fizzle as a result of the containing objects destruction in memory. Used to get similar questions a bit, but I haven't seen one in a while ;)

deQue
08-26-2004, 08:37 PM
Yeah, I got that. thx. ;)