setTimeout in flash 7

As you might know Flash 8 was reinforced with the setTimeout functionality. You can now tell flash 8 player to run a function once after a given milliseconds/interval:

setTimeout(trace, 1000, “string to trace”);

will trace the string “string to trace” after 1 second.

But what if you’re like me, and still need to use flash 7 or even 6, and you also want to enjoy the simplicity of setTimeout. Well I was using my own implementation of setTimeout since flash 6, first with a function and when AS2 came out, I’ve rewriten it into a simple class you can download here.
To use it unzip the ‘net’ folder into your flash classpath if you don’t sure about the classpath just search for it in the flash help or put it in the same directory as your fla. Now you can use it in your flash 7 or above as follows:

import net.guya.Timeout;

Timeout.set(trace, 1000, “string to trace”);

this will do the same as the above function and willl trace the string “string to trace” after 1 second. As with the flash 8 ‘setTimeout’ in the flash 7 ‘Timeout.set’ you can send as many variables as you like after the interval. The trace function only expects one string variable so thats what she gets.

Here is another example:

[kml_flashembed movie=”http://www.guya.net/blogstuff/Timeout/TimeoutSet.swf” height=”400″ width=”340″ /]

You can also pass an object reference to Timeout.set

Timeout.set(this, someFunction, 1000, variabl1, varible2, …variableN);

for example:

import net.guya.Timeout;

var nX:Number = 100;
var nY:Number = 50;
Timeout.set(this, drawLine, 500, nX, nY);

function drawLine(x:Number, y:Number){
   this.lineStyle (2, 0x000000, 100);
   this.moveTo (0, 0);
   this.lineTo (x, y)
}

This program, after 0.5 second will draw a line from the top left to point(100, 50). Here you must send a reference to the object you want the ‘this’ inside the drawLine function to refer to. In this case it’s the MovieClip where the line will be drawn. If no object reference is sent in this case the ‘this’ inside the drawLine will refer to the Timeout object and so no line will be drawn.

A Timeout can be deleted before its function was called with the ‘clear’ method:

var nTimeout1:Number = Timeout.set(trace, 2000, “I will never show”);

Timeout.clear(nTimeout1);

Nothing will be tarced because the Timeout is deleted before it has a chance to run the trace function.

Download the Timeout Class.

< update >

setTimeout in flash 6

After some feedback I realized I should have written this function also, you only need to copy and paste it in your flash 6 files:

*_global.setTimeout = function (o, func, ms){
   var a = new Array();
   for(var i = 3; i *

and use it like this:

setTimeout(this, trace, 1000 , “string to trace”);

Download the setTimeout for Flash 6 function.

< /update >

Guy A

Read more posts by this author.