View Full Version : setInterval?
blank01
01-20-2011, 05:04 AM
For some reason in the past month or so I've been noticing a lot of people posting code using the setInterval functions. Before I first saw somebody post it I never even realized there was such code.
I just want to know whether that function, for what it actually accomplishes, is more effective than using the actual Timer class or even other more basic methods?
Is it good practice to use the interval methods in code? I just wonder since I had never even heard of it before.
maskedMan
01-20-2011, 02:07 PM
In AS3, no it isn't good practice at all.
If you're seeing it a lot in recent months, it's probably either a large body of outdated tutorials, or perhaps it's AS3 written by JavaScript coders who don't know the language well enough to realize there's a better way.
TomMalufe
01-20-2011, 02:08 PM
Instead of using the setInterval() method, consider creating a Timer object, with the specified interval, using 0 as the repeatCount parameter (which sets the timer to repeat indefinitely)
setInterval() exists in a lot of different programing languages and there is nothing wrong with using it in Flash/AS3. But AS3 is supposed to be more of an event driven language and so Adobe recommends using the Timer class and handling the events from that instead.
I think of setInterval as a lazy alternative to the Timer class. For some experiments it just seems easier to write that one line of code rather then the 5-6 lines it would take to set up your timer. For the most part, I do not use setInterval in any official paid projects. Only when I'm messing around with small new ideas.
setInterval is like XMLDocument (known as XML in AS2), they are deprecated function/class that Adobe kept in AS3 to help AS2 coder make the transition. They will be removed from the language at one point so in a way it is bad practice to use them because one day they won't be supported by future version of the Flash Player and as a coder you will have to start working with other Timer and XML class anyway so better doing it now.
Ravi Bhadauria
02-20-2011, 09:59 AM
Hi all,
One more example on setInterval in action script 3.0, i found when i was playing with timing events of as3.0.
Expect, following code will be helpful:
Just create a movieclip on the stage and put its instance name my_btn and copy the following code and paste it on first frame of your flash file:
function moveBtn():void {
my_btn.x+=10;
}
var myInterval:uint=setInterval(moveBtn,500);
my_btn.addEventListener(MouseEvent.MOUSE_DOWN, stopMe);
my_btn.addEventListener(MouseEvent.MOUSE_UP, playMe);
function stopMe(e:MouseEvent):void {
clearInterval(myInterval);
}
function playMe(e:MouseEvent):void {
myInterval =setInterval(moveBtn,500);
}
Thanks,
Ravi Bhadauria
ADMEC Multimedia Institute
#32035055
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.