Newest Articles

MegaCombs
Flash Media Player
XML Driven Pie Chart
Base Defender
Hangman Game
8 Ball Pool


Popular Articles

True Fullscreen Flash Mode
Mp3 Player with XML Playlist
Image Slider
Flash Media Player
3d Rotating Image Cube
Catapult Game


Random Articles

Photo Reel
Focus in on an Image
Alert Box
Space Shooter Game
iPod Style Wheel Slider
Disappearing Button


Links

Foundation-Flash
MickM
TutorialQuest
Tutorialsphere.com - Free Online Tutorials
Newgrounds
TWiT
Link to SwfSpot
Swf Spot



rss feed

onEnterFrame vs setInterval

onEnterFrame vs setInterval
AddThis Social Bookmark Button
Description: Using setInterval instead of onEnterFrame to execute code at different intervals of time
Author: John Bezanis
Added: November 14th 2007
Version: Flash 8


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
Comments
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
whatever, SetInterval set an MILESECOND interval.

DIvides 1000 by the frames p/ second and you will know what is the interval to use.

Exemple:

33.3333 mileseconds = 30 frames p/ second
66.66666... mileseconds = 15 frames p/ second!
is NOT equal!
October 6th 2009 07:10PM   -   Gravyerd
Short and clean tutorial. Good job John.
My vote goes to setInterval, because its flexibility and independency to timelinem, and cpu usage...
October 22nd 2009 08:10AM   -   Best Flash Stock
Wow this is so awesome!! Cheers bro!
October 24th 2009 03:10PM   -   go go hamster toys
great
November 6th 2009 09:11AM   -   vigrx
That is an awesome cube, thanks for sharing.
December 13th 2009 07:12AM   -   dental
Nice easy and clean tutorial. Thank you John. Great one.
December 30th 2009 01:12AM   -   racing car games
Using setInterval function set interval is foundation of action script and this above example is very useful.
January 4th 2010 04:01AM   -   jobs
Nice step by step process from beginning to end. Thank you for sharing your knowledge with us. Keep posting.
January 16th 2010 05:01AM   -   Magento Customization
Wow, I sure would like to know the meaning behind the symbols. I suspect they carry a message.
We must also reflect and draw the correct conclusions
January 17th 2010 05:01AM   -   desktop wallpapers
Thank you for sharing this tips it's great.
January 19th 2010 07:01AM   -   book
SetInterval function make interval setting is so easy.
January 19th 2010 08:01AM   -   film
Thanks for sharing useful tips
January 19th 2010 08:01AM   -   Acai Berry
setInterval is exactly what I as looking for. I was using onEnterFrame and it wasn't doing the job.
January 20th 2010 08:01PM   -   Speed Dating in Chicago
Original code. Thanks
January 21st 2010 12:01PM   -   war airplanes spot
It's nice tutorial , some of these can be truly useful.
January 24th 2010 06:01AM   -   evening dresses
onEnterFrame vs setInterval
January 24th 2010 10:01AM   -   Ares 6
Special thank for good post.
February 5th 2010 09:02PM   -   à¹‚หลดเพลงà
Thank for the information
February 6th 2010 07:02AM   -   à¹‚หลดเพลงm
Special thank for good post.
February 10th 2010 04:02AM   -   à¹‚หลดเพลงà
Hi all. Money frees you from doing things you dislike. Since I dislike doing nearly everything, money is handy. Help me! Could you help me find sites on the: Quit smoking techniques. I found only this - <a href="http://www.other-minds.com/Members/QuitSmoking">free quit smoking programs</a>. Quit smoking, it is not light to stop for those that want trying health that in still 20 situations after quitting you can very like from a caused way addict and food. Quit smoking, my pressure is to thank it down to zero, however as i have opposed, the addiction of being social to smoke in the addiction is the common non-smoker doing me to use without a body for a &quot of lot. Thanks for the help :confused:, Anders from Iran.
February 18th 2010 04:02AM   -   Anders
Add a Comment
name:
website (optional):
captcha type the characters into the box
message (5000 characters or less):