When I wrote a project yesterday, I used a delayed call. I did not know that the flash API had such a function, so I wrote |
Code
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; // function for delayed execution
10 private var Timer: 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
The method name is setTimeout (). The method for canceling the call is cleartimeout (). The example of reference to the help document is as follows:
Code
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 }
At the same time, it is found that there is an interval call and cancellation, and the function
Interval call: setinterval () Cancel call: clearinterval ()
Usage:
Today, I checked the help group documentation and occasionally found that the flash API had a latency call and the method to cancel the latency call.
Code
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
Note that if the two methods and the Timer class are not canceled or stopped, even if you delete their objects, the object will occupy the memory.