如何在spark textarea(as3 + flex4)中一次停止触发两个事件

时间:2011-10-12 16:54:36

标签: actionscript-3 events flex flex4 flex-spark

textChanged和valueCommit两个事件监听器都附加了一个spark textarea,如下所示:

addEventListener("textChanged", 
    function(event:Event):void {                                    
        colorize();                 
},false,0,true);

addEventListener("valueCommit",
    function(event:Event):void {                    
        colorize();                 
},false,0,true);

如果我在textarea中键入任何内容,则会调用此colorize()函数两次。如何阻止这个事件不应该同时触发。请帮忙

1 个答案:

答案 0 :(得分:1)

如果你想听打字,为什么你有两个听众?如果您确实需要两个侦听器,则需要使用colorizesetTimeout进行排队,而不是直接调用它:

import flash.utils.setTimeout;

private var colorizeQueued:Boolean = false;
private function queueColorize():void
{
    if (colorizeQueued)
        return;

    colorizeQueued = true;
    setTimeout(function():void
    {
        // Process for real and note update
        colorize();
        colorizeQueued = false;
    }, 100);
}


addEventListener("textChanged", 
    function(event:Event):void {                                    
        queueColorize();                 
},false,0,true);

addEventListener("valueCommit",
    function(event:Event):void {                    
        queueColorize();           
},false,0,true);