AS3 如何设置最大时间计数

AS3 How to Set Up A Max Time Count Up

我有以下递增代码,但不确定如何包含 if else 语句以使递增停止在 15 秒处。

代码如下:

    var timer:Timer = new Timer(100);
    timer.start(); 
    timer.addEventListener(TimerEvent.TIMER, timerTickHandler);
    var timerCount:int = 0;

    function timerTickHandler(Event:TimerEvent):void{
       timerCount += 100;
       toTimeCode(timerCount);
    }

    function toTimeCode(milliseconds:int) : void {
        //create a date object using the elapsed milliseconds
        var time:Date = new Date(milliseconds);

        //define minutes/seconds/mseconds
        var minutes:String = String(time.minutes);
        var seconds:String = String(time.seconds);
        var miliseconds:String = String(Math.round(time.milliseconds)/100);

        //add zero if neccecary, for example: 2:3.5 becomes 02:03.5
        minutes = (minutes.length != 2) ? '0'+minutes : minutes;
        seconds = (seconds.length != 2) ? '0'+seconds : seconds;

        //display elapsed time on in a textfield on stage
        timer_txt.text = minutes + ":" + seconds+"." + miliseconds;
    }

首先,为了提高效率,您可以使用 currentCount 属性 中内置的计时器来了解已经过了多少时间(而不是为此制作一个 timerCount 变量)

要在 15 秒后停止计时器,只需设置适当的重复计数使其在 15 秒时结束,或者在 15 秒后在滴答处理程序中停止它:

var timer:Timer = new Timer(100,150); //adding the second parameter (repeat count), will make the timer run 150 times, which at 100ms will be 15 seconds.
timer.start(); 
timer.addEventListener(TimerEvent.TIMER, timerTickHandler);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerFinished); //if you want to call a function when all done

function timerTickHandler(Event:TimerEvent):void{
   toTimeCode(timer.delay * timer.currentCount); //the gives you the amount of time past

   //if you weren't using the TIMER_COMPLETE listener and a repeat count of 150, you can do this:
   if(timer.delay * timer.currentCount >= 15000){
       timer.stop();
       //do something now that your timer is done
   }
}