Here's a timer abstraction that I use. My most frequent gotcha with
setTimeout / setInterval is the scope of the function you pass in.
Here, the scope is always the timer object:

function Timer( fn, fd ) {
    var self = this,
        clock;

    function update(){
        self.frameCount++;
        fn.call(self);
    }

    this.frameDuration = fd || 25 ;
    this.frameCount = -1 ;
    this.start = function(){
        update();
        clock = setInterval(update, this.frameDuration);
    };
    this.stop = function(){
        clearInterval( clock );
        clock = null;
    };
}

Set it going with:

var thing = new Timer( function, frameDuration );
thing.start();

I'm not sure I see the need to include timers in the core, because the
plugin architecture is there for you to adapt jQuery specifically to
your project.

For an events-based way of animating (using the code above), check
out:

http://webdev.stephband.info/events/frame/

It allows you to start an animation timer simply by binding a 'frame'
event to something.  The act of binding starts the timer, and the
bound function gets called on every frame.

--

You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to jquery-...@googlegroups.com.
To unsubscribe from this group, send email to 
jquery-dev+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en.


Reply via email to