dcsimg
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: AS3 Timer + Date - CountDown and CountUp need help

Hybrid View

  1. #1
    Junior Member
    Join Date
    Jul 2006
    Posts
    21

    AS3 Timer + Date - CountDown and CountUp need help

    Hi,

    I need help getting this class to work correctly, I have attached a working flex project that compiles just fine in case someone wanted to quickly import it into their Flex dev.

    basically all I am trying to do is to use the timer and the date class to simulate an accurate [alarm/stopwatch/whatever], I want to display the time as it is counting down and also as it is counting up for a custom duration, in this instance 5 minutes.

    here is a preview of the class. I just need to know the correct way of calculating the time elapsed and the time left.

    your help is much appreciated.

    Thanking you in advance,

    PHP Code:
    package view
    {
        
    import flash.utils.Timer;
        
    import mx.containers.Canvas;
        
    import flash.events.TimerEvent;
        
    import mx.controls.Label;
        
    import mx.core.Application;

        public class 
    counterExampleBase  extends Application
        
    {
            private var 
    daysElapsed:            int 0;
            private var 
    hoursElapsed:            int 0;
            private var 
    minutesElapsed:            int 0;
            private var 
    secondsElapsed:            int 0;
            private var 
    millisecondsElapsed:        int 0;

            private var 
    daysLeft:                int 0;
            private var 
    hoursLeft:                int 0;
            private var 
    minutesLeft:            int 0;
            private var 
    secondsLeft:            int 0;
            private var 
    millisecondsLeft:            int 0;        

            private var 
    timeStart:                Date;
            private var 
    oneSecondTimer:            Timer;
            private var 
    isTicking:                Boolean false;

            public static const 
    MILLISECONDS_PER_MINUTE:    int 1000 60;
            public static const 
    MILLISECONDS_PER_HOUR:    int 1000 60 60;
            public static const 
    MILLISECONDS_PER_DAY:    int 1000 60 60 24;

            private var 
    noOfMinutes:            int MILLISECONDS_PER_MINUTE;
            
            
            [
    Bindable]
            public var 
    lblCountUp:                Label;
            
            [
    Bindable]        
            public var 
    lblCountDown:            Label;
            
            public function 
    counterExampleBase()
            {

            }

                    protected function 
    onCreationComplete():void
                    
    {
                        
    Start();
                    }
                    
            public function 
    Start():void
            
    {
                
    timeStart    = new Date();
                
                
    timeStart.setTimenoOfMinutes );            

                
    oneSecondTimer     = new Timer(1000);

                
    oneSecondTimer.addEventListener(TimerEvent.TIMERonTimer);
                
    oneSecondTimer.addEventListener(TimerEvent.TIMER_COMPLETEonTimerComplete);                        
                
                
    oneSecondTimer.start();
            }

            private function 
    onTimer(event:TimerEvent):void
            
    {        
                
    // I am not sure how to do the calculations for the time counting down and time counting up. i.e time left and time elapsed.
                            
                
    var today:Date = new Date();                    
                var 
    delta:Number timeStart.getTime() - today.getTime();

                
    daysLeft Math.floordelta MILLISECONDS_PER_DAY );
                
    delta -= daysLeft MILLISECONDS_PER_DAY;

                
    hoursLeft Math.floordelta MILLISECONDS_PER_HOUR );
                
    delta -= hoursLeft MILLISECONDS_PER_HOUR;

                
    minutesLeft Math.floordelta MILLISECONDS_PER_MINUTE );
                
    delta -= minutesLeft MILLISECONDS_PER_MINUTE;

                
    secondsLeft Math.floordelta 1000 );

                
    // obvously the following 2 lines are not working correctly yet.
                
                // it should start counting down from 0:0:5:0 to 0:0:0:0
                
    lblCountDown.text  daysLeft ":" hoursLeft ":" minutesLeft ":" secondsLeft;
                
                
    // it should start counting up from 0:0:0:0 to 0:0:5:0
                
    lblCountUp.text  daysElapsed ":" hoursElapsed ":" minutesElapsed ":" secondsElapsed;            
            }

            private function 
    onTimerComplete(event:TimerEvent):void
            
    {
                
    trace("Time's Up!");
            }
        }

    Attached Files Attached Files
    Last edited by Flexer; 06-05-2007 at 07:35 AM.

  2. #2
    Junior Member
    Join Date
    Jul 2006
    Posts
    21
    any help, clues please !

    Thanks

  3. #3
    Junior Member
    Join Date
    Jul 2006
    Posts
    21
    I got it, thanks anyway...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center