flex计时器 - 显示消失

时间:2011-07-20 11:59:59

标签: flex timer

我有一个flex的对象 - 我希望在点击一个按钮时出现,10秒后消失或对象关闭,如果按下相同的按钮则重新出现。

我尝试过这样的事情:

public class my_obj
{
  private var _myTimer: Timer;

     public function my_obj()
     {
           _myTimer = new Timer(10000);

     }
     private function init(): void
     {
           _myTimer.addEventListener(TimerEvent.TIMER, onTimeout);
            _myTimer.start();
     }

     private function onTimerTimeOut(event: TimerEvent): void
     {
        dispatchEvent(new Event(CLOSE_EVENT));
     }
}
我错过了什么? 谢谢!

1 个答案:

答案 0 :(得分:0)

工作正常。这是工作代码示例:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:layout>
        <s:VerticalLayout/>
    </s:layout>
    <s:Panel width="100%" height="400" creationComplete="my_obj()">
        <s:layout>
            <s:VerticalLayout/>
        </s:layout>
        <fx:Script>
            <![CDATA[
                private var _myTimer: Timer;

                public function my_obj():void
                {
                    _myTimer = new Timer(10000);

                }
                private function init(): void
                {
                    toggleBtn.visible = true;

                    _myTimer.addEventListener(TimerEvent.TIMER, onTimeout);
                    _myTimer.start();
                }

                private function onTimeout(event: TimerEvent): void
                {
                    toggleBtn.visible = false;
                    //dispatchEvent(new Event(CLOSE_EVENT));
                }

                private function hideIt(): void
                {
                    toggleBtn.visible = false;

                }
            ]]>
        </fx:Script>
        <s:Button label="Some Button" click="init()"/>
        <s:Button id="toggleBtn" label="Appeared on 'Some Button' click. Click to close it." click="hideIt()" visible="false"/>
    </s:Panel>
</s:WindowedApplication>