Global Timer in Javascript with Multiple Callbacks

Posted by Mike Beepo on Stack Overflow See other posts from Stack Overflow or by Mike Beepo
Published on 2010-04-23T13:23:25Z Indexed on 2010/04/23 13:53 UTC
Read the original article Hit count: 370

Filed under:
|
|

I want to create a global timer object in javascript and then be able to add callbacks to it on the fly. This way I can just use one global timer in my script to execute all actions at a certain interval rather than wasting resources by using multiples.

This is how I want to be able to piece things together:

var timer = new function() { 
 clearInterval( this.interval );

 //[1] At this point I want the Callbacks to be run

 var self = this;
 setTimeout(function() {self.timer()}, 200);
}

function otherObject = new function() {
    //When created I want to bind my object's function called cb to the global timer at [1]
}

otherObject.prototype.cb = function() {
    //Stuff that should be done every time the timer is run
}

var someObject = new otherObject();

How would I make it possible bind any number functions (most of which are functions within other objects) to run at the interval of my timer on the fly?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about timers