开发者

flex countdown algorithm

The algorithm must take in an Int-value of the number of secon开发者_如何学运维ds remaining (ex. 2005), then convert and return a "0-padded-String" of the hours, minutes, and seconds remaining (ex. 02:35:15).

I have another event handler that will invoke the above algorithm change in seconds (count down).


Here is an implementation for your conversion method:

public static function Seconds2HHMMSS(Time:Number):String 
    {
        var hours:int =int(int(Time/60)/60);
        var hoursZeroPadding:String = "";
        if (hours<10)
            hoursZeroPadding="0";

        var minutes:int =int(Time/60)%60;
        var minutesZeroPadding:String = "";
        if (minutes<10)
            minutesZeroPadding="0";

        var seconds:int =Time%60;
        var secondsZeroPadding:String = "";
        if (seconds<10)
            secondsZeroPadding="0";

        var result:String =    hoursZeroPadding + hours.toString()
                            + minutesZeroPadding + minutes.toString()
                             + secondsZeroPadding + seconds.toString();
        return result;
    }

The opposite conversion is quite simpler:

    public static function HHMMSS2Seconds(Time:String):int 
    {   
        var result:int =   int(Time.substr(0,2))*3600 
                         + int(Time.substr(2,2))*60 
                         + int(Time.substr(4,2));   
        return result;
    }


Use div and mod and your knowledge of how many seconds in an hour / min / second to come up with each of the groupings. You can then do a padding of zeros with an if statement (or the ?: operator)

Edit: or just use Dunaril's code, I didn't want to just give you the code without you having tried something first, that goes against the spirit of SO for me :P

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜