·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]AS3中setInterval、setTimeout与Timer类

时间:2013-02-18 23:46CuPlayer.com
在AS3中保留了原始的用法,setInterval()与setTimeout()依然是可以使用的,跟AS2.0有所不同的是增加了,Timer()方法

在AS3中保留了原始的用法,setInterval()与setTimeout()依然是可以使用的,跟AS2.0有所不同的是增加了,Timer()方法。

时间间隔可以用 setInterval 命令来创建并用 clearInterval 命令来终止。setInterval 所用的参数有两种格式。在第一种格式中,你传递给 setInterval 的参数可以是一个函数名,一段时间上的间隔以及一些传递给前面函数的相关参数。当 setInterval 运行时它会依照规定的时间间隔依次将列出的参数传递给指定的函数,直到你调用 clearInterval 将其终止。

1、原始方法:

setInterval(closure:Function, delay:Number, ... arguments):uint
setTimeout(closure:Function, delay:Number, ... arguments):uint

注意返回类型是uint,所在CLASS:flash.utils.*

2、新方法:

  1. package 
  2. { 
  3. import flash.display.Sprite; 
  4. import flash.events.TimerEvent; 
  5. import flash.utils.Timer; 
  6.  
  7. public class ShortTimer extends Sprite 
  8. { 
  9. public function ShortTimer() 
  10. { 
  11. // creates a new five-second Timer 
  12. var minuteTimer:Timer = new Timer(1000, 5); 
  13.  
  14. // designates listeners for the interval and completion events 
  15. minuteTimer.addEventListener(TimerEvent.TIMER, onTick); 
  16. minuteTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete); 
  17.  
  18. // starts the timer ticking 
  19. minuteTimer.start(); 
  20. } 
  21.  
  22. public function onTick(evt:TimerEvent):void 
  23. { 
  24. // displays the tick count so far 
  25. // The target of this event is the Timer instance itself. 
  26. trace("tick " + evt.target.currentCount); 
  27. } 
  28.  
  29. public function onTimerComplete(evt:TimerEvent):void 
  30. { 
  31. trace("CuPlayer.com提示:Time's Up!"); 
  32. } 
  33. } 
  34. } 
  35.  
  36. CuPlayer.com提示输出结果如下: 
  37.  
  38. tick 1 
  39. tick 2 
  40. tick 3 
  41. tick 4 
  42. tick 5 
  43. CuPlayer.com提示:Time's Up! 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器