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();
}
Pay due attention to the error you got: it says that a
Function
is a type and thataddEventListener()
wants it. Although your listener returns void, it is aFunction
! So, what about returning the listener?Simple like this. It works for any kind of event. And no closure issues.
Note on
removeEventListener()
:Don't try to do
timer.removeEventListener(TimerEvent.TIMER, saveChat(username, chatBoxText))
. It won't recognize your function because you'll be passing a new one on it every time you usesaveChat()
. Instead, just take its reference out into a variable and you're done:You can try this:
A function called by a listener can only have one argument, which is the event triggering it.
You can get around this by having the function called by the event call another function with the required arguments:
Another thing you can do create a custom event class that extends
flash.events.Event
and create properties that you need within.Then you can dispatch this with properties defined:
And listen for it:
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:
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/
Actually, you can pass additional parameters to an event listener without creating a custom event by using Actionscript's dynamic function construction.
When setting up the closeHandler for the Alert window we call the addArguments() method and pass in an array continaing all of the parameters we want to pass to the closeHandler. The addHandler() method will return the function we will call when the Alert window closes with the parameters included.