Flash Player 6.
setInterval(functionName:Function,interval:Number[,param1:Object,param2, ...,paramN]): Number
functionName A function name or a reference to an anonymous function.
interval The time in milliseconds between calls to the functionName parameter.
param1, param2, ..., paramN Optional parameters passed to the function or methodName parameter.
An identifying integer that you can pass to clearInterval() to cancel the interval.
Function; 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 less than the SWF file's frame rate (for example, 10 frames per second [fps] is equal to 100 milliseconds), the interval function is called as close to interval as possible. You must use the updateAfterEvent() function to make sure that the screen refreshes often enough. 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.
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. Both calls to setInterval() send the string "interval called" to the Output panel every 1000 milliseconds.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.macromedia.com/software/central/images/klynch_breezo.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);
delete me;
};
Then create a FLA in the same directory called User.as. 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.");
}
}
clearInterval(), updateAfterEvent()
RSS feed | Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/mx2004/main_7_2/00001662.html
Comments
Francis Cheng said on Aug 16, 2004 at 10:06 AM : rowdypig said on Dec 1, 2004 at 11:16 AM : webweber@bluewin.ch said on Jan 7, 2005 at 5:34 AM :