zoukankan      html  css  js  c++  java
  • Actionscript(flash)中的延迟调用和间隔调用

    2010-04-29 16:42

    昨天写项目时有个地方用到了延时调用,我不知道flash api中自带这种功能于是自己写了一个

    代码
    1 package com.zs
    2 {
    3  import flash.display.Sprite;
    4  import flash.events.TimerEvent;
    5  import flash.utils.Timer;
    6
    7  public class AppWaiter
    8 {
    9 private var _delayFunction:Function; //延时执行的函数
    10 private var timer:Timer; //定时器
    11
    12 public function AppWaiter(guest:Sprite)
    13 {
    14 }
    15
    16 public function callLater(seconds:Number,callFun:Function):void
    17 {
    18 _delayFunction = callFun;
    19 timer = new Timer(seconds*1000,1);
    20 timer.addEventListener(TimerEvent.TIMER,callDelayFuntion);
    21 timer.start();
    22 }
    23
    24 public function stopCallLater():void
    25 {
    26 if(timer){
    27 timer.stop();
    28 timer.removeEventListener(TimerEvent.TIMER,callDelayFuntion);
    29 timer = null;
    30 }
    31 }
    32
    33 private function callDelayFuntion(e:TimerEvent):void
    34 {
    35 _delayFunction.call();
    36 stopCallLater();
    37 }
    38 }
    39 }
    40
    41

    方法名是:setTimeout() ,取消调用的方法是clearTimeout();具体使用方法引用帮助文档的例子

    代码
    1 package {
    2 import flash.display.Sprite;
    3 import flash.utils.*;
    4
    5 public class ClearTimeoutExample extends Sprite {
    6 private var delay:Number = 1000; // delay before calling myDelayedFunction
    7 private var intervalId:uint;
    8 private var count:uint = 1000000;
    9
    10 public function ClearTimeoutExample() {
    11 intervalId = setTimeout(myDelayedFunction, delay);
    12 startCounting();
    13 }
    14
    15 public function startCounting():void {
    16 var i:uint = 0;
    17 do {
    18 if(i == count-1) {
    19 clearTimeout(intervalId);
    20 trace("Your computer can count to " + count + " in less than " + delay/1000 + " seconds.");
    21 }
    22 i++;
    23 } while(i < count)
    24 }
    25
    26 public function myDelayedFunction():void {
    27 trace("Time expired.");
    28 }
    29 }

    同时发现还有一个间隔调用和取消,函数

    间隔调用:setInterval () 取消调用:clearInterval ()

    使用方法:

    今天查帮组文档偶尔发现,原来flash api自带了一个延时调用,和取消延时调用的方法

    代码
    1 package {
    2 import flash.display.Sprite;
    3 import flash.utils.*;
    4
    5 public class ClearIntervalExample extends Sprite {
    6 private var intervalDuration:Number = 1000; // duration between intervals, in milliseconds
    7 private var intervalId:uint;
    8 private var counter:uint = 0;
    9 private var stopCount:uint = 3;
    10
    11 public function ClearIntervalExample() {
    12 intervalId = setInterval(myRepeatingFunction, intervalDuration, "Hello", "World");
    13 }
    14
    15 public function myRepeatingFunction():void {
    16 trace(arguments[0] + " " + arguments[1]);
    17
    18 counter++;
    19 if(counter == stopCount) {
    20 trace("Clearing Interval");
    21 clearInterval(intervalId);
    22 }
    23 }
    24 }
    25 }
    26

    需要注意的是,这两个方法和Timer类如果不取消,和停止,即使你删除了他们所在的对象,该对象还会占用着内存。

  • 相关阅读:
    Socket实现Web应用的本质
    板凳要坐十年冷 代码不写一句空
    Python写的刷QB钓鱼盗号软件
    除法细节_Python小知识点00005
    标识符,变量的命名_Python小知识00003
    类和ID选择器的区别
    十分钟搞定CSS选择器
    HTML引入CSS样式三种方法及优先级
    CSS代码语法
    html form <label>标签基础语法结构与使用案例教程
  • 原文地址:https://www.cnblogs.com/crkay/p/1747974.html
Copyright © 2011-2022 走看看