setTimeOut:设置超时时间,只会执行一次!
setInterval:设置一个间隔时间,间隔多久会触发一次!除非 remove,否则会永久执行下去!
Timer:设置一个间隔时间,有次数限制,只执行制定次数!
参数说明:
setTimeOut(函数名,延迟时间,参数);
setInterval(函数名,延迟时间,参数);
Timer(延迟时间,执行次数);
举例:
1.setTimeOut
- package {
- import flash.display.Sprite;
- import flash.utils.*;
-
- public class SetTimeoutExample extends Sprite {
- private var delay:Number = 1000;
- public function SetTimeoutExample() {
- var intervalId:uint = setTimeout(myDelayedFunction, delay, "Hello", "World");
- }
- public function myDelayedFunction():void {
- trace(arguments[0] + " " + arguments[1]);
- }
- public function clearTimeout():void {
- if(intervalId>0) {
- clearTimeout(intervalId);
- }
- }
- }
- }
2.setInterval
- package {
- import flash.display.Sprite; import flash.utils.*;
- public class SetIntervalExample extends Sprite {
- private var intervalDuration:Number = 1000;
- private var intervalId:uint;
- private var counter:uint = 0;
- private var stopCount:uint = 3;
- public function SetIntervalExample() {
- intervalId = setInterval(myRepeatingFunction, intervalDuration, "Hello", "World");
- }
- public function myRepeatingFunction():void {
- trace(arguments[0] + " " + arguments[1]);
- counter++;
- if(counter == stopCount) {
- trace("Clearing Interval");
- clearInterval(intervalId);
- }
- }
- }
- }
3.Timer
- package {
- import flash.utils.Timer;
- import flash.events.TimerEvent;
- import flash.display.Sprite;
- public class TimerExample extends Sprite {
- public function TimerExample() {
- var myTimer:Timer= new Timer(1000, 2);
- myTimer.addEventListener("Timer", timerHandler);
- myTimer.start();
- }
- public function timerHandler(event:TimerEvent):void {
- trace("timerHandler: " + event);
- }
- }
- }