设定时间运行函数方法:setInterval与clearInterval

setInterval

() 函数  
public function setInterval(closure:Function , delay:Number , ... arguments):uint
语言版本:  ActionScript 3.0
运行时版本:  AIR 1.0 Flash Player 9

以指定的间隔(以毫秒为单位)运行函数。

作为使用 setInterval() 方法的替代方法,请考虑创建一个 Timer 对象,带有指定的间隔,并使用 0 作为 repeatCount 参数(这样可将计时器设置为无限制重复)。

如果打算使用 clearInterval() 方法取消 setInterval() 调用,请确保将 setInterval() 调用分配给一个变量(clearInterval() 函数稍后将引用该变量)。如果不通过调用 clearInterval() 函数取消 setInterval() 调用,则不会将包含 setTimeout 闭包函数的对象作为垃圾回收。

参数


closure :Function — 要执行的函数的名称。不要包括引号或圆括号,并且不要指定要调用的函数的参数。例如,使用 functionName ,而不要使用 functionName()functionName(param)
 

delay :Number — 间隔(以毫秒为单位)。
 

... arguments — 传递给 closure 函数的可选参数列表。

返回

uint — 超时进程的唯一数字标识符。使用此标识符可通过调用 clearInterval() 方法取消进程。

另请参见


示例  ( 如何使用本示例 )

以下示例使用 setInterval() 方法创建一个计时间隔,以 1 秒的固定间隔调用 myRepeatingFunction() 方法。
package {
    import flash.display.Sprite;
    import flash.utils.*;

    public class SetIntervalExample extends Sprite {
        private var intervalDuration:Number = 1000; // duration between intervals, in milliseconds
        
        public function SetIntervalExample() {
            var intervalId:uint = setInterval(myRepeatingFunction, intervalDuration, "Hello", "World");
        }

        public function myRepeatingFunction():void {
            trace(arguments[0] + " " + arguments[1]);
        }
    }
}

clearInterval

() 函数
public function clearInterval(id:uint ):void
语言版本:  ActionScript 3.0
运行时版本:  AIR 1.0 Flash Player 9

取消指定的 setInterval() 调用。

参数


id :uint — 设置为变量的 setInterval() 调用的 ID,如以下代码中所示:

另请参见


示例  ( 如何使用本示例 )

以下示例使用 setInterval() 方法创建一个计时间隔,以 1 秒的固定间隔调用 myRepeatingFunction() 方法。

每次调用 myRepeatingFunction 方法都会递增 counter 属性,当该属性等于 stopCount 属性时,调用 clearInterval() 方法(使用属性 intervalId 进行调用,该属性是对前面创建的间隔的引用 ID)。

package {
    import flash.display.Sprite;
    import flash.utils.*;

    public class ClearIntervalExample extends Sprite {
        private var intervalDuration:Number = 1000; // duration between intervals, in milliseconds
        private var intervalId:uint;
        private var counter:uint     = 0;
        private var stopCount:uint     = 3;
        
        public function ClearIntervalExample() {
            intervalId = setInterval(myRepeatingFunction, intervalDuration, "Hello", "World");
        }

        public function myRepeatingFunction():void {
            trace(arguments[0] + " " + arguments[1]);

            counter++;
            if(counter == stopCount) {
                trace("Clearing Interval");
                clearInterval(intervalId);    
            }
        }
    }
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值