¿Cómo anulas esto?
Simplemente utilizamos la clase Fecha también en una secuencia de comandos que contará correctamente el tiempo.
- Crear un nuevo documento AS3
- añadir 3 cuadros de texto con nombre minText, secText, MilText y un botón de llamada start_btn
- añadir este código en el primer fotograma:
var STT: int; // utilizamos esta variable para hacer un seguimiento de la hora de inicio como una marca de tiempo
var myTimer: Timer = new Timer (1); // este es nuestro temporizador
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);
}
¿Por qué necesita marcas de tiempo y no usa las funciones de la clase Date?
Porque si se desea contar la diferencia entre dos eventos es probable que utilice esto:
endEvent.seconds - startEvent.seconds
que es muy malo si el evento de inicio ocurre en el segundo 57'th y el evento final que ocurre en el 17º segundo, obtendrías -40 segundos en lugar de 20, y así sucesivamente.
'flash.utils.getTimer()' es más eficiente, según otra respuesta – BadFeelingAboutThis