How to pass arguments into event listener function in flex/actionscript?

Viewed 28544

Since when using sql lite if you try and do a function at the same moment it throws an error, im just trying to make a function that will check if its executing, and if it is try again in 10 milliseconds, this exact function works fine if i dont have to pass any arguments to the function but im confused how I can pass the vars back into the function it'll be executing.

I want to do:

timer.addEventListener(TimerEvent.TIMER, saveChat(username, chatBoxText));

But it will only allow me to do:

timer.addEventListener(TimerEvent.TIMER, saveChat);

It gives me this compile error:

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

How can I get this to pass this limitation?

Here's what I've got:

public function saveChat(username:String, chatBoxText:String, e:TimerEvent=null):void
{
    var timer:Timer = new Timer(10, 1);
    timer.addEventListener(TimerEvent.TIMER, saveChat);

    if(!saveChatSql.executing)
    {
        saveChatSql.text = "UPDATE active_chats SET convo = '"+chatBoxText+"' WHERE username = '"+username+"';";
        saveChatSql.execute();
    }
    else timer.start();
}
5 Answers

Above calling saveChat(arg, arg, arg) it's not for me, i need to pass arguments that i dont have in this method but i have another solution

Im always using additional method passParameters to adding new arguments:

public function passParameters(method:Function,additionalArguments:Array):Function
{return function(event:Event):void{
    method.apply(null, [event].concat(additionalArguments));}
}

explanation of this is here - its simple and always work http://sinfinity.pl/blog/2012/03/28/adding-parameters-to-event-listener-in-flex-air-as3/

Related