我有以下计数代码,但我不知道如何能够包含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;
}
答案 0 :(得分:0)
首先,为了提高效率,您可以使用currentCount
属性中构建的计时器来了解已经过了多长时间(而不是为此目的设置timerCount
var)
要在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
}
}