2

I wonder if you can get the number of milliseconds that have elapsed since an actionscript timer has started.

I want to set a simple label in flex that has the value of how long it wil take before the timer fires (again).

this is the code, at 'GET ELAPSED TIME', I need the time that the timer has been running (since it last fired the function):

var timer = new Timer (10000);
timer.addEventListener(TimerEvent.TIMER, foo);
timer.start();

var numberOfSeconds = timer.delay-timer.'GET ELAPSED TIME';

resetLabel.text = "only "+numberOfSeconds+" until foo fires";

Does this function to get the timers time exist and how is it called (google failed to answer me)?

1
  • Do You know (new Date()).getTime() function ? And if You like to do such simple interval , read about flash.utils.setInterval and flash.utlis.clearInterval functions , easier than timer .
    – turbosqel
    Feb 23, 2012 at 14:09

2 Answers 2

2

There is no real property to get the remaining time of your Timer. A workaround could be to add a check within your foo function. A basic setup would look something like this:

var realDelay:int = 10000;
var timer:Timer = new Timer (1);
timer.addEventListener(TimerEvent.TIMER, foo);
timer.start();

function foo(e:TimerEvent) : void 
{
    var realCount:int = Math.floor(timer.currentCount/realDelay);
    var timeLeft:Number = (realDelay-timer.currentCount%realDelay);
    resetLabel.text = "Real count: " + realCount + ", milliseconds left: " + timeLeft; 
}
2
  • the realDelay is the timers delay I guess?
    – Hans Vn
    Feb 23, 2012 at 18:00
  • Some variation on this appears to be the only way to make this work. I would recommend using getTimer() to get the time since the Flash Player VM started to store start time and then compare times, however, instead of the currentCount method used here.
    – Sam DeHaan
    Feb 23, 2012 at 21:34
2

Unfortunately the build in Timer does not have support for what you want. Here is a very simple wrapper class to get an overall duration much like Sam DeHaan's suggestion.

package utility
{
    import flash.utils.Timer;
    import flash.utils.getTimer;

    /**
     * DurationTimer
     * Description:
     * Extending Timer class to help show elapsed time
     **/
    public class DurationTimer extends Timer
    {
        private var _startTime:int;
        private var _endTime:int;

        public function DurationTimer( delay:Number, repeatCount:int = 0 )
        {
            super( delay, repeatCount );
        }

        public function get duration():int
        { 
            return ( running ) ? getTimer() - _startTime : _endTime - _startTime;
        }

        override public function start():void
        {
            _startTime = getTimer();
            super.start();
        }

        override public function stop():void
        {
            _endTime = getTimer();
            super.stop();
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.