简体   繁体   English

弹性文字变更活动

[英]Flex text change event

I'm tracking how fast does the text of a textArea change. 我正在跟踪textArea的文本更改速度。 If it changes faster than 500 ms then i don't want to do anything, but if it doesn't change in 500 ms, i want to call a method. 如果它的变化速度超过500毫秒,那么我什么都不愿做,但是如果它在500毫秒内没有变化,我想调用一个方法。 I tried like this: 我这样尝试过:

public function textchangeListener(e : Event):void
        {
            if(((new Date).getTime() - changeTime)>500)
            {
                prepareText();
            }

            changeTime = (new Date).getTime();
        }

This method is the event handler for text change. 此方法是文本更改的事件处理程序。 But the problem is, if it changes only under 500 ms and after that it doesn't change, then my method won't be called. 但是问题是,如果它仅在500毫秒内改变,并且此后它没有改变,那么我的方法将不会被调用。 I make this for a better performance, so the prepareText() is called only when the user stops typing for 500 ms. 我这样做是为了获得更好的性能,因此仅当用户停止键入500毫秒时才调用prepareText()。

How about this... 这个怎么样...

Once you get the first text change event you can call a procedure like textTimeOut(). 一旦获得第一个文本更改事件,就可以调用诸如textTimeOut()之类的过程。 It will essentially work like this. 它将基本上像这样工作。

function textTimeOut():void
{
    start a timer for 500 ms
    set an event listener for it (your prepareText() function)
    if textTimeOut is called again before the timer gets to 0,
        reset the timer to 500 ms
}

I would use a setTimeout in the event handler and reset it everytime it changes: 我将在事件处理程序中使用setTimeout,并在每次更改时将其重置:

var changeTimeout:Number = -1
function handler(e:Event):void {
    if(changeTimeout != -1)
        clearTimeout(changeTimeout)
    changeTimeout = setTimeout(function():void{
        changeTimeout = -1
        prepareText();
    }, 500)
}

So I used a timer. 所以我用了一个计时器。 Thanks for the advice. 谢谢你的建议。 This is what the end result is: 这是最终结果是:

    protected var timer:Timer = new Timer(300);

public function AdvancedTextArea()
        {
            super();
            this.addEventListener(Event.CHANGE,textchangeListener);
            timer.addEventListener(TimerEvent.TIMER,prepareText);
            timer.repeatCount = 1;

        }
public function textchangeListener(e : Event):void
        {
            if(timer.running)
            {
                timer.stop();
            }
            timer.start();

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM