由于您可能正在使用 actionscript 3,因此我建议您根本不要使用间隔。 Timer 对象可能会更好,因为它可以提供更好的控制,例如能够设置它在停止自身之前触发的次数,并且能够根据需要轻松启动、停止和重新启动计时器。
使用 Timer 对象并为每个刻度添加事件侦听器的示例
import flash.utils.Timer;
import flash.events.TimerEvent;
// each tick delay is set to 1000ms and it'll repeat 12 times
var timer:Timer = new Timer(1000, 12);
function timerTick(inputEvent:TimerEvent):void {
trace("timer ticked");
// some timer properties that can be accessed (at any time)
trace(timer.delay); // the tick delay, editable during a tick
trace(timer.repeatCount); // repeat count, editable during a tick
trace(timer.currentCount); // current timer tick count;
trace(timer.running); // a boolean to show if it is running or not
}
timer.addEventListener(TimerEvent.TIMER, timerTick, false, 0, true);
控制定时器:
timer.start(); // start the timer
timer.stop(); // stop the timer
timer.reset(); // resets the timer
它抛出两个事件:
TimerEvent.TIMER // occurs when one 'tick' of the timer has gone (1000 ms in the example)
TimerEvent.TIMER_COMPLETE // occurs when all ticks of the timer have gone (when each tick has happened 11 times in the example)
API 文档:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/Timer.html