View comments | RSS feed

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():

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.

Note: Always 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


Version 8

Comments


Francis Cheng said on Sep 13, 2005 at 12:07 PM :
Flash 8 also introduces a new global function named setTimeout(), which is similar to setInterval(), except that setTimeout() is used when you only need to call a function once. Unfortunately, it was inadvertently omitted from the documentation, but a description and example of setTimeout() is posted as a LiveDocs comment on the main Global Functions page:

http://livedocs.macromedia.com/flash/8/main/00001717.html
SerendipityTaylor said on Jan 17, 2006 at 10:34 PM :
Note when using the syntax: setInterval(functionReference:Function, interval:Number):Number ; do not use quotation marks when referencing your function as you would for the other syntax

Example:

intervalId = setInterval(myFunction, 100);

function myFunction():Void{
trace("Interval trace");
clearInterval(intervalId);
};
rphaedrus said on Jul 24, 2006 at 10:41 AM :
Using Flash 8 Pro, Publishing to Flash Player 7.

If I set an interval for 2147483648 ms or more (24 days, 20 hours, 31 minutes, 23 seconds, 648ms), it fires immediately.

eg: _nIntervalId = setInterval(this,"myFunction",2147483648 )

If I set the interval for 2147483647 ms or less, it appears to behave as expected.
No screen name said on Oct 10, 2006 at 7:45 PM :
My example shows how u can launch counters with a simple function "counter" that has :name,interval, delay
this is useful if u wanna launch dynamic counters with the name generated on the moment

function cron(id) {
count = eval(id+"count");
maxCount =eval(id+"maxc");
if(count >= maxCount) {
clearInterval(eval(id));
}
count++;
trace(count);
set (id+"count",count);
}


function counter(cname,n,ms){
a=cname;
set(a+"count",1);
set(a+"maxc",n);
set(a,setInterval(this, "cron", ms,a));
}

counter('mycounter',2,100);
PleaseKING said on Oct 19, 2006 at 2:54 AM :
The interesting story is what happens if I hibernate my computer and re-open it later? How many times the function would be called? Once or hibernate period / frequency?
Sleeve said on Nov 12, 2006 at 5:39 PM :
The example given in the documentation regarding the correct use of setInterval() in a custom Actionscript 2.0 class does not work as it should. Instead use the following example:

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
{
intervalID = setInterval(this, "executeCallback", duration)
if (intervalID%2 > 0)
{
trace("intervalID " + intervalID+" is extra and was cleared")
clearInterval(intervalID);
}
}
public function executeCallback():Void
{
trace("executeCallback intervalId: " + intervalId + " count: " + count);
if(count >= maxCount) {
clearInterval(intervalId);
}
}
count++;
}
aheera1 said on Nov 22, 2006 at 2:32 AM :
i need to display images one by one at equal intervals by calling the images from xml database in flash if i play autoplay button and get stopped wen i pre next or back button? Any1 who can help me out
No screen name said on Jan 29, 2007 at 12:11 AM :
mistake ? in Example 4 :
Constructor function CustomClass() works only without :void
juankpro said on Aug 1, 2007 at 9:25 AM :
Number greater than 2147483647 does not work because the setInterval receives only 32-bit signed integers. For this type of integer the number 2147483648 is interpreted as -2147483648 (in two's complement notation) so negative number are rejected and taken as 0.

The number 2147483649 is -2147483647 and so on til -1.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/8/main/00001766.html