Applications
Because setInterval is similar to an onEnterFrame event,
technically speaking, you could run your entire movie
off of a, or many, setInterval calls. This is, however,
a little unorthodox, so it isn't commonly done. What
setInterval is best at is simple (non-accurate)
count-down timers and other (again, non-accurate)
time-specific actions.
- displayTime = 30;
- countDown = function(message){
- displayTime--;
- if (displayTime == 0){
- clearInterval(timer);
- trace("Times Up!");
- }
- }
- timer = setInterval(countDown, 1000);
Here, every second the displayTime is decremented by
1 until reaching 0 where "Times Up!" is traced. Once 0,
the countDown function also clears out its own interval
as not to cause the timer to continuously count down
below 0 into negative numbers.
You may want to be careful with using this for
certain timers though, especially with games. Knowing
that setInterval is not frame dependant means that you
could be giving your users with slower computers a
disadvantage as they would obviously not be able to
perform the same amount of actions within any given time
period because of a slow frame rate. In that case, its
best to create a frame based timer.
Another use for setInterval is making a specific
movieclip play its frames outside the frame rate of the
main timeline.In essence, this can make a movieclip in a
movie with a 12 FPS frame rate appear to play at 5 FPS
or even 30. When combined with updateAfterEvent(),
clearInterval can refresh the screen before the next
frame is drawn allowing you to show something faster
than that which is possible within the set frame rate.
Example:
- fastFrames = function(){
- myMovie_mc.nextFrame();
- updateAfterEvent();
- }
- speedyID = setInterval(fastFrames, 40);
With an interval of 40 milliseconds, the movieclip
myMovie_mc will play its frames at about 25 frames per
second despite the set frame rate of the movie. However,
it is important to know that Flash still has its
limitations. If you try to set an interval that is too
fast for Flash to keep up with, your set time will not
be met, instead, Flash will only play the interval as
fast as it can, not dropping function calls to match
with actual timing.
Loading swfs is another practical use for
setInterval. The reason for this is because you may have
certain operation-specific events associated with your
onEnterFrame events and might not want to have to mess
with them in order to create a preloader or loader
checker for any of your loaded movies. With setInterval
being what it is and operating itself, by itself, it
allows you to make a unique repeating checker function
which can detect when a movie has loaded and then
perform the desired reactions. Here, setInterval is the
method used to instantiate a type of onLoad for a loaded
movie, only here, we aren't waiting for the entire
loaded movie to load, just for 10 frames, so when they
are available, they can be navigated to. Example:
- loadMovieNum("loadme.swf",1);
- checkLoaded = function(){
- if (_level1._framesloaded >= 10){
- _level1.gotoAndStop(10);
- clearInterval(intervalID);
- }
- }
- intervalID = setInterval(checkLoaded, 50);
Using setInterval, we can also mimic setTimeout
function in javascript by creating a setInterval
function which will only call the passed function once
and then clear itself out automatically with
clearInterval:
- _global.setTimeout = function(a,b,c, args){
- // for a basic function call:
- if (typeof a == "function"){
- args = arguments.slice(2);
- var ID, func = function(){
- a.apply(null, args);
- clearInterval(ID);
- }
- ID = setInterval(func, b, args);
- // for an object method call:
- }else{
- args = arguments.slice(3);
- var ID, func = function(){
- a[b].apply(a, args);
- clearInterval(ID);
- }
- ID = setInterval(func, c, args);
- }
- return ID;
- }
- _global.clearTimeout = clearInterval;
This can be used just as setInterval normally is,
only this will run the function only once after the
given interval has passed. Because setInterval is used
to create this setTimeout variation, clearInterval will
also interrupt it, though a clearTimeout was also made
for consistency; which itself is just a copy of
clearInterval.
Here's one that will pass the interval ID to the
function so you can access it within that function and
clear it if you need to:
- _global.sendInterval = function(a,b,c, args){
- // for a basic function call:
- if (typeof a == "function"){
- args = arguments.slice(2);
- var ID, func = function(){
- a.apply(null, args);
- }
- ID = setInterval(func, b, args);
- // for an object method call:
- }else{
- args = arguments.slice(3);
- var ID, func = function(){
- a[b].apply(a, args);
- }
- ID = setInterval(func, c, args);
- }
- return args[args.length] = ID;
- }
Works on much of the same premise as setTimeout only
the ID, instead of being cleared automatically, is
passed into the function call as the last argument in
that function. So for whatever function is used, the
last argument passed will be the interval ID and can be
cleared at the discrepancy of that function.
Conclusion
SetInterval, though a new and exciting feature of Flash
MX, really doesn't offer too much to get overly excited
about. The fact that it is not frame dependant can throw
a lot of things off track for a well oiled Flash movie
machine. However, that fact alone is part of
setIntervals real power, especially in exact time
calculation which was clumsy with the use of getTimer or
the date object in Flash 5. Now actions within Flash can
be easily performed at exact time intervals with little
ease. What functions you run like that is truly up to
you.
Senocular