setInterval(functionName:Object, interval:Number [, param1:Object, param2, ..., paramN]) : Number setInterval(objectName:Object, methodName:String, interval:Number [, param1:Object, param2, ..., paramN]) : Number
Calls a function or a method or an object at periodic intervals while a SWF file plays. You can use an interval function to update variables from a database or to update a time display.
If interval is greater than the SWF file's frame rate, the interval function is only called each time the playhead enters a frame; this minimizes the impact each time the screen is refreshed.
Note: In Flash Lite 2.0, the interval passed into this method is ignored if it is less than the SWF file's frame rate and the interval function is called on the SWF file's frame rate interval only. If the interval is greater than the SWF file's frame rate, the event is called on the next frame after the interval has elapsed.
functionName:Object - A function name or a reference to an anonymous function.
interval:Number - The time in milliseconds between calls to the functionName or methodName parameter.
param:Object [optional] - Parameters passed to the functionName or methodName parameter. Multiple parameters should be separated by commas: param1,param2, ...,paramN
objectName:Object - An object containing the method methodName.
methodName:String - A method of objectName .
Number - An identifying integer that you can pass to clearInterval() to cancel the interval.
Usage 1: The following example calls an anonymous function every 1000 milliseconds (1 second).
setInterval( function(){ trace("interval called"); }, 1000 );
Usage 2: The following example defines two event handlers and calls each of them. The first call to setInterval() calls the callback1() function, which contains a trace() statement. The second call to setInterval() passes the "interval called" string to the function callback2() as a parameter.
function callback1() {
trace("interval called");
}
function callback2(arg) {
trace(arg);
}
setInterval( callback1, 1000 );
setInterval( callback2, 1000, "interval called" );
Usage 3: This example uses a method of an object. You must use this syntax when you want to call a method that is defined for an object.
obj = new Object();
obj.interval = function() {
trace("interval function called");
}
setInterval( obj, "interval", 1000 );
obj2 = new Object();
obj2.interval = function(s) {
trace(s);
}
setInterval( obj2, "interval", 1000, "interval function called" );
You must use the second form of the setInterval() syntax to call a method of an object, as shown in the following example:
setInterval( obj2, "interval", 1000, "interval function called" );
When working with this function, you need to be careful about the memory you use in a SWF file. For example, when you remove a movie clip from the SWF file, it will not remove any setInterval() function running within it. Always remove the setInterval() function by using clearInterval() when you have finished using it, as shown in the following example:
// create an event listener object for our MovieClipLoader instance
var listenerObjectbject = new Object();
listenerObject.onLoadInit = function(target_mc:MovieClip) {
trace("start interval");
/* after the target movie clip loaded, create a callback which executes
about every 1000 ms (1 second) and calls the intervalFunc function. */
target_mc.myInterval = setInterval(intervalFunc, 1000, target_mc);
};
function intervalFunc(target_mc) {
// display a trivial message which displays the instance name and arbitrary text.
trace(target_mc+" has been loaded for "+getTimer()/1000+" seconds.");
/* when the target movie clip is clicked (and released) you clear the interval
and remove the movie clip. If you don't clear the interval before deleting
the movie clip, the function still calls itself every second even though the
movie clip instance is no longer present. */
target_mc.onRelease = function() {
trace("clear interval");
clearInterval(this.myInterval);
// delete the target movie clip
removeMovieClip(this);
};
}
var jpeg_mcl:MovieClipLoader = new MovieClipLoader();
jpeg_mcl.addListener(listenerObject);
jpeg_mcl.loadClip("http://www.helpexamples.com/flash/images/image1.jpg",
this.createEmptyMovieClip("jpeg_mc", this.getNextHighestDepth()));
If you work with setInterval() within classes, you need to be sure to use the this keyword when you call the function. The setInterval()function does not have access to class members if you do not use the keyword. This is illustrated in the following example. For a FLA file with a button called deleteUser_btn, add the following ActionScript to Frame 1:
var me:User = new User("Gary");
this.deleteUser_btn.onRelease = function() {
trace("Goodbye, "+me.username);
clearInterval(me.intervalID);
delete me;
};
Then create a file called User.as in the same directory as your FLA file. Enter the following ActionScript:
class User {
var intervalID:Number;
var username:String;
function User(param_username:String) {
trace("Welcome, "+param_username);
this.username = param_username;
this.intervalID = setInterval(this, "traceUsername", 1000, this.username);
}
function traceUsername(str:String) {
trace(this.username+" is "+getTimer()/1000+" seconds old, happy birthday.");
}
}