Flex Timer example

2019-06-09 18:42发布

问题:

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

Here is what I want to achieve:

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

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 ?

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

回答1:

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
}


回答2:

The example you're referring to is making things a bit to complicated for your particular problem. Here is a little example of how you can show the minutes with a simple Timer:

private const TIMER_INTERVAL:Number = 1000*60;
private var t:Timer;

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

private function updateTimer(evt:TimerEvent):void {

    counter.text = String(t.currentCount);
}

private function startTimer():void {

    t.start();
}

private function stopTimer():void {

    t.stop();

    // if you want the timer to reset make sure to reset your text also
    /*
    t.reset();
    counter.text = "0";
    */
}