onEnterFrame vs setInterval
Description: Using setInterval instead of onEnterFrame to execute code at different intervals of time
Author: John Bezanis
Added: November 14th 2007
Version: Flash 8
Total Views: 5362
Views in the Past 7 Days: 127
onEnterFrame is a popular method to have code run at certain intervals of time, but it has a drawback. onEnterFrame's time intervals are equal to the frame rate set in the document properties. If you want to run some code at different intervals of time, onEnterFrame won't be able to do the job. This is where setInterval is used. setInterval works like onEnterFrame except the time intervals are independent of the document's set frame rate. The timer measures the intervals in milliseconds, so you can run a function many times faster or slower that the document's frame rate.
Example: We have two boxes, one using onEnterFrame and one using setInterval. The document is set to 30 frames per second. The box using setInterval is set to execute every 100th of a second.
Initializing the two boxes to 0:
onenterframebox=0;
setintervalbox=0;
Code for onEnterFrame:
onEnterFrame=function(){
onenterframebox++;
}
Function that will run at each interval:
function intervalfunction():Void {
setintervalbox++;
}
When creating the actual interval, intervalid is an id associated with the interval, the first parameter "this" is the object to associate with the interval, intervalfunction is the function that gets called at each interval, and 10 is the number of milliseconds between each interval.
var intervalid:Number = setInterval(this, "intervalfunction", 10);
To remove the interval, use
clearInterval(intervalid);
The source code of the example is available below:
Download the Source File
Questions and Suggestions
Hello,
I am contacting you on behalf of www.flashcomponents.net . I've read one of your tutorials and I like the way you write. Our site is continuously growing and we recently added a tutorial section. We kindly ask for your approval to allow us to publish your tutorials on our site, mentioning you as the author.
Of course, we are inviting you to do it yourself, but either way, it would be our pleasure to publish them.
Kind regards,
Mike | Flash Components Team
Mail: flashcomponents@hotmail.com
February 11th 2008 10:02AM - Mike
Hi,
For the most flexibility you are probably better off using a Timer:
http://livedocs.adobe.com/flex/3/langref/flash/utils/Timer.html
Cheers,
Brindy
June 10th 2008 03:06AM - Brindy
Add a Question or Suggestion







