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

ketan
  • 19,129
  • 42
  • 60
  • 98
Hans Vn
  • 787
  • 1
  • 16
  • 32
  • 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 '12 at 14:09

2 Answers2

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();
        }
    }
}
haysclark
  • 1,080
  • 8
  • 16
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; 
}
Rick van Mook
  • 2,065
  • 1
  • 12
  • 16
  • the realDelay is the timers delay I guess? – Hans Vn Feb 23 '12 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 '12 at 21:34