setInterval or onEnterFrame… your thoughts?

This is by no means a new subject of discussion, but I’ve been thinking lately about how some people use setInterval more and some people use onEnterFrame more. I personally like to use setInterval mainly because I have control over the speed at which the function is called as opposed to having to rely on the frame rate. I know that there are instances where one or the other might be specifically called for, but which do you prefer to use more often and why?

http://flash.fincanon.com/wp-content/plugins/sociofluid/images/digg_48.pnghttp://flash.fincanon.com/wp-content/plugins/sociofluid/images/reddit_48.pnghttp://flash.fincanon.com/wp-content/plugins/sociofluid/images/dzone_48.pnghttp://flash.fincanon.com/wp-content/plugins/sociofluid/images/stumbleupon_48.pnghttp://flash.fincanon.com/wp-content/plugins/sociofluid/images/delicious_48.pnghttp://flash.fincanon.com/wp-content/plugins/sociofluid/images/furl_48.pnghttp://flash.fincanon.com/wp-content/plugins/sociofluid/images/technorati_48.pnghttp://flash.fincanon.com/wp-content/plugins/sociofluid/images/myspace_48.pnghttp://flash.fincanon.com/wp-content/plugins/sociofluid/images/facebook_48.pnghttp://flash.fincanon.com/wp-content/plugins/sociofluid/images/yahoobuzz_48.png





14 Responses to “setInterval or onEnterFrame… your thoughts?”

  1. Chris Long Says:

    Well, I was always a fan of onEnterFrame until I started working in the advertising world. Basically, because of changes in artisitc direction, we were constantly having to change the speed at which things happend. setInterval allows you do this quickly.

  2. Josh Tynjala Says:

    Arguably, both methods can give the same results if you’re calculating the time on each interval or frame and doing some math based on how much time has passed. That’s not always the case, but it’s possible.

    Personally, I prefer setInterval (and the Timer class in AS3). I like it better because it doesn’t have to rely on the framerate. You might not necessarily see every update, but if you decide to alter the framerate later, you won’t have to make as many changes.

  3. ericd Says:

    it depends on what you’re doing. setInterval can save a lot of player cycles.

  4. Peter Elst Says:

    I prefer setInterval in most cases, having code execute on every frame using onEnterFrame is overkill for a lot of situations. Think about it, having your function called 12 or more times a second is pretty intensive.

    For a lot of purposes I just have an interval running every 500 ms — for everything other than tweening that works a charm.

    The Timer class in AS3 is awesome though, wonder if someone made an implementation for AS2 yet.

  5. JesterXL Says:

    If I need to know when each frame happens, I’ll use onEnterFrame since it’s the only function that will fire every frame consistently. setInterval can, but sometimes more, sometimes less; no guarentees whereas with onEnterFrame, you are guarenteed you’re code will run. This is useful when tracking the status of animations, and is in fact the only way for frame animated MovieClips.

    For everything else, setInterval.

  6. JesterXL Says:

    Also, even though it’s deprecated in AS3, setTimeout is still pimp for one-shot timers.

  7. Keith Peters Says:

    As I said to Sam the other day, “you’ll get my enterframe when you pry it from my cold dead hands.” But seriously, I use enterframe mostly out of habit. And in AS2, it was a lot quicker to set up, just make a function called onEnterFrame and it runs. In AS3, you have to set up the listener and import the Event class anyway, so my last excuse is gone and I’ll probably start using intervals more often.

  8. senocular Says:

    onEnterFrame mostly. Most of what I use it for is motion. Motion and framerate go hand in hand. Framerate and onEnterFrame go hand in hand. onEnterFrame and motion go hand in hand.

  9. ericd Says:

    I’ve fallen in LOVE with setTimeout… :(

  10. Josh Tynjala Says:

    Like Jesse mentioned, setTimeout is great. If you’d prefer to use a Timer in AS3, the constructor has a parameter that defines how many times the Timer runs. That’s pretty nice.

  11. ericd Says:

    package {
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.display.Sprite;

    public class TimerExample extends Sprite {

    public function TimerExample() {
    var myTimer:Timer = new Timer(1000, 2);
    myTimer.addEventListener(”timer”, timerHandler);
    myTimer.start();
    }

    public function timerHandler(event:TimerEvent):void {
    trace(”timerHandler: ” + event);
    }
    }
    }

  12. guya Says:

    Intervals in flash are also inaccurate and are influenced from outside the flash player. So even if you use intervals you’ll never get perfect control.

    http://blog.guya.net/2006/05/08/flash-player-inaccuracy-clarified/
    http://blog.guya.net/2006/03/14/check-your-interval/

  13. John Says:

    What about using updateAfterEvent(), this has been added to the timer event so you can cause your display to redraw after the timer tick.
    e.g.

    private function timerHandler(e:TimerEvent):void{
    draw();
    e.updateAfterEvent();
    }

    So apparently you can have your swf running at 1fps but your timer will cause it to draw.

    Has anybody tried this? I am currently playing with and the other methods mentioned with my 3d engine(http://johnukmn.blogspot.com/2006/08/introducing-lux.html)

  14. Stacked Stone Says:

    Like you I also also like to use setInterval to control over the speed at which the function is called.

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word