Creating a (correct) time counter in AS3

How to create a time counter in as3? A simple google search will point you to an AS3 class timer, which is actually an event counter and not a good time .

I saw this http://blogs.adobe.com/pdehaan/2006/07/using_the_timer_class_in_actio.html and I am a little worried that this is the official documentation that should work.

Q: Where exactly is the problem?

A: The Timer class performs operations on the event stack, and if you have a rather heavy application, I can bet that the timer will distort your time if you use it to count seconds, milliseconds or something else.

+5
source share
2

, getTimer() (flash.utils.getTimer), , Flash-. StopWatch:

public class StopWatch {
    private var _mark:int;
    private var _started:Boolean = false;

    public function start():void { _
        mark = getTimer(); 
        _started = true;
    }

    public function get elapsed():int { 
        return _started ? getTimer() - _mark : 0; 
    }
}

:

+5

?

script, .

  • AS3
  • 3 minText, secText, MilText start_btn
  • :

var stt: int;// ,
var myTimer: Timer = (1);//

var starttime:Date; // pretty obvious
var actualtime:Date; // pretty obvious

myTimer.addEventListener(TimerEvent.TIMER, stopWatch); // we start counting with this counter

start_btn.addEventListener(MouseEvent.CLICK, startClock); // add a button listener to start the timer

function startClock(event:MouseEvent):void
{
    starttime = new Date(); // we get the moment of start
    stt = int(starttime.valueOf().toString()); // convert this into a timestamp
    myTimer.start(); // start the timer (actually counter)
}

function stopWatch(event:TimerEvent):void
{
    actualtime = new Date(); // we get this particular moment       
    var att:int = int(actualtime.valueOf().toString()); // we convert it to a timestamp

    // here is the magic
    var sec:int = (Math.floor((att-stt)/1000)%100)%60; // we compute an absolute difference in seconds
    var min:int = (Math.floor((att-stt)/1000)/60)%10; // we compute an absolute difference in minutes
    var ms:int = (att-stt)%1000; // we compute an absolute difference in milliseconds

    //we share the result on the screen
    minText.text = String(min);
    secText.text = String(sec);
    milText.text = String(ms);
}

Date?

, , , , :

     endEvent.seconds - startEvent.seconds

, 57- , 17- , -40 20 ..

+1

All Articles