Flex的定时例如Flex

2023-09-09 21:47:05 作者:月下独酌

我想使用定时器弯曲。我提到的这个例子: 时间:blog.flexexamples.com

I am trying to use timer in flex. I referred to this example : Timer : blog.flexexamples.com.

这是我想要实现:

我要开始计时,显示出分钟过去了由于计时器   启动。它应该是独立的,你是在该地区。(   无论任何区域,您都在,计时器应该罚款   每个区域)。

I want to start the timer, showing minutes elapsed since timer started. It should be independent of the region you are in.( irrespective of whatever zone you are in, timer should work fine in every zone).

定时器应该继续,除非被点击一些按钮,在这里我想   显示在经过几分钟,在一个警告框的时间,然后计时器应该再次从0开始启动。

Timer should continue, unless some button is clicked, where I want to show the time elapsed in minutes, in an Alert Box and then timer should start again from 0 onwards.

我想我的例子,但它不能正常工作。

I tried my example, but it is not working properly.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                layout="vertical"
                verticalAlign="middle"
                backgroundColor="white"
                creationComplete="init()">

    <mx:Script>
        <![CDATA[
            import flash.events.TimerEvent;
            import flash.utils.Timer;

            import mx.controls.Alert;
            private const TIMER_INTERVAL:Number = 10;

            private var baseTimer:int;

            private var t:Timer;

            private function init():void {
                t = new Timer(TIMER_INTERVAL);
                t.addEventListener(TimerEvent.TIMER, updateTimer);
            }

            private function updateTimer(evt:TimerEvent):void {
                var d:Date = new Date(getTimer()-baseTimer);
                var min:String = (d.minutes).toString();
                var sec:String = (d.seconds).toString();

                counter.text = String(min+"."+sec);
            }

            private function startTimer():void {

                baseTimer = getTimer();
                t.start();
            }

            private function stopTimer():void {
                t.stop();
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Button label="Start timer" click="startTimer()" />
        <mx:Button label="Stop timer" click="stopTimer()" />
    </mx:ApplicationControlBar>

    <mx:Label id="counter" fontSize="96" />
</mx:Application>

谁能告诉是什么问题?怎么解决呢?

Can somebody tell what is the problem ? How to solve it ?

编辑: 如果我在我的电脑上运行这个例子中,定时器从30.0直到它到达59.59,然后它变成回到0.0,然后重新开始......现在,我要的是从0.0开始一直演到一些按钮计​​时分钟点击...这应该工作在任何时区

EDIT : If I run this example on my pc, timer starts from 30.0 till it reaches 59.59 and then it turns back to 0.0 and then starts again......Now What I want is to start from 0.0 and continue counting minutes till some button is clicked ... and this should work in any time zones

推荐答案

你的使用情况并不需要使用日期()和/或时区。所有你需要做的是记录经历的秒数,并定时为您提供一个简单的方法来做到这一点:设置间隔为1000(每秒计数),并使用 Timer.currentCount 。然后,所有你需要做的就是计算分,秒显示。下面是一个实现,您可以将您现有的MXML:

Your use case does not require the use of Date() and/or time zones. All you need to do is count the seconds elapsed, and Timer offers you an easy way to do this: Set the interval to 1000 (One count per second) and use Timer.currentCount. Then all you need to do is calculate minutes and seconds for display. Below is an implementation you can incorporate into your existing mxml:

private function init():void {
    t = new Timer(1000);
    t.addEventListener(TimerEvent.TIMER, updateTimer);
}

// it's good practice to separate event handler from functional method
private function updateTimer(evt:TimerEvent):void {
    display (t.currentCount);
}

private function display ( count : int ) : void {
    var minutes : int = count / 60; // divide by 60 to get the minutes
    var seconds : int = count % 60; // use modulo operator to get the "rest"
    var min : String = minutes < 10 ? "0" + minutes : "" + minutes; // add leading zero if necessary
    var sec : String = seconds < 10 ? "0" + seconds : "" + seconds;

    counter.text = min+":"+sec; // no need to cast to String if you use "" + something
}

private function startTimer():void {
    t.start();
}

private function stopTimer():void {
    t.stop();
    t.reset();
    display (0); // reset the display
}