如何让定时器倒计时 AS3

How to have Timer Count Down AS3

我有一个计时器,我正在尝试倒计时而不是倒计时。几年前我使用过这段代码,但似乎无法弄清楚如何让它从 5 分钟开始倒计时。

我只试过弄乱变量。

这是我的变量等:

tCountTimer = new Timer(100);
        tCountTimer.addEventListener(TimerEvent.TIMER, timerTickHandler);
        timerCount = 0;

        tCountTimer.start();

这是我要计算的函数:

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

    private function toTimeCode(milliseconds:int):void 
    {
        this.milliseconds = milliseconds;


        //create a date object using the elapsed milliseconds
        time = new Date(milliseconds);

        //define minutes/seconds/mseconds
        minutes = String(time.minutes + 5);
        seconds = String(time.seconds);
        miliseconds = 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
        playScreen.timeLimitTextField.text = minutes + ":" + seconds;


    }

它算得上是完美的,但就是无法将时间从 5 分钟缩短到现在。非常感谢所有帮助。

没有测试,但我希望这个想法是绝对清楚的。

// Remember the time (in milliseconds) in 5 minutes from now.
var endTime:int = getTimer() + 5 * 60 * 1000;

// Call update function each frame.
addEventListener(Event.ENTER_FRAME, onFrame);

function onFrame(e:Event):void
{
    // How many milliseconds left to target time.
    var aTime:int = endTime - getTimer();

    // Fix if we are past target time.
    if (aTime < 0) aTime = 0;

    // Convert remaining time from milliseconds to seconds.
    aTime /= 1000;

    // Convert the result into text:
    // aTime % 60 = seconds (with minutes stripped off)
    // aTime / 60 = full minutes left
    var aText:String = ze(aTime / 60) + ":" + ze(aTime % 60);

    // Do whatever you want with it.
    trace(aText);
}

// Function to convert int to String
// and add a leading zero if necessary.
function ze(value:int):String
{
    return ((value < 10)? "0": "") + value.toString();
}