Zane Clark
Zane Clark

Reputation: 53

Any way to Call A Function As A String?

I've just started a project, and am wondering if you can call a function (in an event listener) through a string.

import flash.utils.Timer;
import flash.events.Event;
import flash.events.TimerEvent;

var threesec:Timer=new Timer(3000, 1);
var whaton:String="tsecc"
threesec.start();
threesec.addEventListener(TimerEvent.TIMER_COMPLETE, whaton);
function tsecc(tsecc:TimerEvent):void{
    trace("Hello")
    threesec.reset();
    threesec.start();
}

This does not work because of this line:

threesec.addEventListener(TimerEvent.TIMER_COMPLETE, whaton);

and this error code:

1067: Implicit coercion of a value of type String to an unrelated type Function.

I know what I am doing is horribly wrong, but is there a correct way to call a function in string format?

Do I have to add a property to the variable, do I have to create another type of variable?

Upvotes: 0

Views: 106

Answers (1)

HITMAN
HITMAN

Reputation: 548

threesec.addEventListener(TimerEvent.TIMER_COMPLETE, this[whaton]);  

I used "bracket syntax" to do this. You can learn more about it by searching on the Internet.

Upvotes: 4

Related Questions