Search code examples
actionscript-3eventsapache-flexflex4flex-spark

how to stop triggering two event at a time in spark textarea (as3+flex4)


textChanged and valueCommit both event listener are attached with a spark textarea as follows:

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

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

if I type any thing in textarea, then this colorize() function is called twice. How can I stop this one that both event should not be triggered together. Pls help


Solution

  • If you want to listen for typing, why do you have two listeners? If you really need two listeners, you need to queue colorize with a setTimeout instead of calling it directly:

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