Flash CS4 Professional ActionScript 2.0

setInterval function

setInterval(functionReference:Function, interval:Number, [param1:Object, param2, ..., paramN]) : Number
setInterval(objectReference:Object, methodName:String, interval:Number, [param1:Object, param2, ..., paramN]) : Number

Calls a function or a method of an object at periodic intervals while a SWF file plays. You can use setInterval() to execute any function repetitively over time.

Use the following tips when working with setInterval():

  • Identify the scope of the function being called.
  • Identify the scope where the interval ID (the return value of setInterval()) was set.
  • Clear previously set intervals before starting new ones.

These tips are discussed in further detail in the following paragraphs.

Identify the scope of the function being called. To identify the scope of the function being called, pass the object where the setInterval() method can execute (the object scope) as the first parameter and the method name you want to execute as the second parameter (as shown in the second signature). This ensures that the desired method is executed from the scope of the object reference passed in. When the method is executed in this manner, it can reference member variables on the object using the this keyword.

Identify the scope where the interval identifier was set. To identify the scope where the interval identifier (intervalId) was set, you can assign it to a member variable on the object scope that you pass to setInterval(). In this way, the function being called can locate the interval identifier at this.intervalId.

Clear previously set intervals. To clear previously set intervals before starting new ones, you should usually call clearInterval()before calling setInterval(). This ensures that you do not overwrite or otherwise destroy your intervalId variable, which is the only reference to the currently running interval. To call clearInterval() prior to calling setInterval(), both the initiating script and the script being executed must have access to the intervalId, as shown in the Examples.

NOTEAlways be sure to call clearInterval() when you want the script to stop looping.

Availability: ActionScript 1.0; Flash Player 6

Parameters

functionReference: Function - A reference to the function to be called.

interval: Number - The time in milliseconds between calls to the functionReference or methodName function passed in.

If interval is less than the SWF file's frame rate (for example, 10 frames per second [fps] is equal to 100 millisecond intervals), the interval function is called as close in time to the value of interval as possible. Executing long, memory-intensive scripts during an interval causes delays. If the function being called initiates a change to visual elements, you should 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 called only after interval has expired and the playhead has entered the next frame; this minimizes the impact each time the screen is refreshed.

param: Object [optional] - Parameters passed to the function that was sent to functionReference or methodName. Multiple parameters should be separated by commas: param1,param2, ...,paramN

objectReference: Object - An object that contains the method specified by methodName.

methodName: String - A method that exists in the scope of the object specified by objectReference.

Returns

Number - An integer that identifies the interval (the interval ID), which you can pass to clearInterval() to cancel the interval.

Example

Example 1: The following example traces a message at an interval of 20 milliseconds, up to 10 times, and then clears the interval. The object scope, this, is passed in as the first parameter, and the method name, executeCallback, as the second. This ensures that executeCallback() is executed from the same scope as the calling script.


var intervalId:Number;
var count:Number = 0;
var maxCount:Number = 10;
var duration:Number = 20;

function executeCallback():Void {
 trace("executeCallback intervalId: " + intervalId + " count: " + count);
 if(count >= maxCount) {
 clearInterval(intervalId);
 } 
 count++;
}

intervalId = setInterval(this, "executeCallback", duration);


Example 2: The following example is similar to the first, except that it calls clearInterval() before setInterval(). This can help prevent unwanted loops and is especially important in event-based systems where the initiating script can be executed multiple times before any particular interval has been cleared.


var intervalId:Number;
var count:Number = 0;
var maxCount:Number = 10;
var duration:Number = 20;

function executeCallback():Void {
 trace("executeCallback intervalId: " + intervalId + " count: " + count);
 if(count >= maxCount) {
 clearInterval(intervalId);
 }
 count++;
}

function beginInterval():Void {
 if(intervalId != null) {
 trace("clearInterval");
 clearInterval(intervalId);
 }
 intervalId = setInterval(this, "executeCallback", duration);
}

beginInterval();
beginInterval();
beginInterval();


Example 3: The following example shows how to pass a custom argument to the function that is being called.


var intervalId:Number;
var count:Number = 0;
var maxCount:Number = 10;
var duration:Number = 20;
var colors:Array = new Array("red", 
 "blue", 
 "yellow", 
 "purple", 
 "green", 
 "orange", 
 "salmon", 
 "pink", 
 "lilac", 
 "powder blue", 
 "mint");

function executeCallback(param:String) {
 trace("executeCallback intervalId: " + intervalId + " count: " + count + " param: " + param);
 clearInterval(intervalId);
 if(count < maxCount) {
 count++;
 intervalId = setInterval(this, "executeCallback", duration, colors[count]);
 }
}

if(intervalId != null) {
 clearInterval(intervalId);
}

intervalId = setInterval(this, "executeCallback", duration, colors[count]);


Example 4: The following example shows how to use setInterval() correctly from an ActionScript 2.0 custom class. Note that similar to previous examples, this is passed to the setInterval() function to ensure that the called method is executed within the correct scope.


class CustomClass {
 private var intervalId:Number;
 private var count:Number = 0;
 private var maxCount:Number = 10;
 private var duration:Number = 20;

 public function CustomClass():Void {
 beginInterval();
 }

 private function beginInterval():Void {
 if(intervalId != null) {
 trace("clearInterval");
 clearInterval(intervalId);
 }
 intervalId = setInterval(this, "executeCallback", duration);
 }

 public function executeCallback():Void {
 trace("executeCallback intervalId: " + intervalId + " count: " + count);
 if(count >= maxCount) {
 clearInterval(intervalId);
 }
 count++;
 }
}


In a new document, instantiate a new instance of the new class:


var custom:CustomClass = new CustomClass();


See also

clearInterval function, updateAfterEvent function, class statement