如何将参数传递到事件监听器功能的柔性

如何将参数传递到事件监听器功能的柔性

本文介绍了如何将参数传递到事件监听器功能的柔性/动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用SQL精简版,如果你尝试做一个函数在它抛出一个错误,我只是试图让一个函数,将检查其执行相同的时刻,如果是在10毫秒再试一次,这个确切时,由于功能工作正常,如果我没有通过任何函数的自变量,但即时通讯困惑我怎么能通过瓦尔放回它会被执行功能。

我想要做的:

  timer.addEventListener(TimerEvent.TIMER,saveChat(用户名,chatBoxText));
 

但是,这将只允许我这样做:

  timer.addEventListener(TimerEvent.TIMER,saveChat);
 

它给了我这个编译错误:

我如何能得到这个通过这个限制吗?

下面是我有:

 公共职能saveChat(用户名:字符串,chatBoxText:字符串,E:TimerEvent = NULL):无效
{
    VAR定时器:定时器=新的定时器(10,1);
    timer.addEventListener(TimerEvent.TIMER,saveChat);

    如果(!saveChatSql.executing)
    {
        saveChatSql.text =UPDATE active_chats SET康沃='+ chatBoxText +'其中username ='+用户名+;;
        saveChatSql.execute();
    }
    别的timer.start();
}
 

解决方案

所谓由听众只能有一个参数的函数,是事件触发的。

您可以通过让调用该事件的函数调用另一个函数所需参数解决这个问题:

  timer.addEventListener(TimerEvent.TIMER,_saveChat);
功能_saveChat(E:TimerEvent):无效
{
    saveChat(阿根廷,阿根廷,阿根廷);
}

功能saveChat(ARG1:类型,ARG2:类型,ARG3:型):空白
{
    //你的逻辑。
}
 

您可以做的另一件事创建可扩展的自定义事件类对象类型:flash.events.Event ,并创建你需要在属性。

 包
{
    进口对象类型:flash.events.Event;

    公共类自定义事件扩展事件
    {
        //你的自定义事件类型。
        公共静态常量SAVE_CHAT:字符串=saveChat;

        //你的自定义属性。
        公共变种用户名:字符串;
        公共变种chatBoxText:字符串;

        //构造函数。
        公共功能自定义事件(类型:字符串,气泡:布尔=假,取消:布尔=假):无效
        {
            超(类型,泡沫,取消);
        }
    }
}
 

然后就可以使用性能调度此定义的:

  timer.addEventListener(TimerEvent.TIMER,_saveChat);
功能_saveChat(E:TimerEvent):无效
{
    VAR EVT:自定义事件=新的自定义事件(CustomEvent.SAVE_CHAT);

    evt.username =君子好逑;
    evt.chatBoxText =自定义事件是容易的。

    则dispatchEvent(EVT);
}
 

和听吧:

 的addEventListener(CustomEvent.SAVE_CHAT,saveChat);
功能saveChat(五:自定义事件):无效
{
    跟踪(e.username +:+ e.chatBoxText);
    //输出:马蒂:自定义事件很容易。
}
 

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:

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();
}
解决方案

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:

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.
}

Another thing you can do create a custom event class that extends flash.events.Event and create properties that you need within.

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);
        }
    }
}

Then you can dispatch this with properties defined:

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);
}

And listen for it:

addEventListener(CustomEvent.SAVE_CHAT, saveChat);
function saveChat(e:CustomEvent):void
{
    trace(e.username + ": " + e.chatBoxText);
    // Output: Marty: Custom events are easy.
}

这篇关于如何将参数传递到事件监听器功能的柔性/动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 20:08