因为当使用sql lite如果你尝试在同一时刻执行一个功能,它会抛出一个错误,我只是试图做一个函数,将检查它是否执行,如果它是在10毫秒再次尝试,这个确切的功能工作正常如果我不必传递任何参数到函数,但我困惑我如何可以将vars传递回它将要执行的函数。
我想要做:
- timer.addEventListener(TimerEvent.TIMER,saveChat(username,chatBoxText));
但这只会让我做:
- timer.addEventListener(TimerEvent.TIMER,saveChat);
它给我这个编译错误:
1067: Implicit coercion of a value of
type void to an unrelated type
Function
我如何得到这个通过这个限制?
这里是我有:
- 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();
- }
解决方法
一个侦听器调用的函数只能有一个参数,它是触发它的事件。
listener:Function
— The listener function that processes the event.
This function must accept an Event
object as its only parameter and must
return nothing,as this example
shows:
function(evt:Event):void
07000
您可以通过将事件调用的函数调用另一个具有必需参数的函数来解决此问题:
- timer.addEventListener(TimerEvent.TIMER,_saveChat);
- function _saveChat(e:TimerEvent):void
- {
- saveChat(arg,arg,arg);
- }
- function saveChat(arg1:type,arg2:type,arg3:type):void
- {
- // Your logic.
- }
另一件事你可以创建一个自定义事件类,扩展flash.events.Event并创建您需要的属性。
- package
- {
- import flash.events.Event;
- public class CustomEvent extends Event
- {
- // Your custom event 'types'.
- public static const SAVE_CHAT:String = "saveChat";
- // Your custom properties.
- public var username:String;
- public var chatBoxText:String;
- // Constructor.
- public function CustomEvent(type:String,bubbles:Boolean=false,cancelable:Boolean=false):void
- {
- super(type,bubbles,cancelable);
- }
- }
- }
然后您可以使用定义的属性调度它:
- timer.addEventListener(TimerEvent.TIMER,_saveChat);
- function _saveChat(e:TimerEvent):void
- {
- var evt:CustomEvent = new CustomEvent(CustomEvent.SAVE_CHAT);
- evt.username = "Marty";
- evt.chatBoxText = "Custom events are easy.";
- dispatchEvent(evt);
- }
并听:
- addEventListener(CustomEvent.SAVE_CHAT,saveChat);
- function saveChat(e:CustomEvent):void
- {
- trace(e.username + ": " + e.chatBoxText);
- // Output: Marty: Custom events are easy.
- }