views:

191

answers:

2

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?

A: 

Have the interval fire an event. The subscribing functions can listen to the event (or not) and choose to fire or not according to their own logic.

The jQuery way of doing this would be:

(function() {
  setInterval(function() {
    $(document).trigger('heartbeat-of-the-universe');
  }, 200);
})();

Then later inside otherObject ...

$(document).bind('heartbeat-of-the-universe', this.cb);

There are obviously other ways of implementing events.

As the google link in the comments notes, this isn't the option with the highest performance. It is flexible and relatively forgiving however.

wombleton
+1  A: 

Create a GlobalTimer object and give it the ability to register callback functions and cancel itself.

function makeGlobalTimer(freq) {
  freq = freq || 1000;

  // array of callback functions
  var callbacks = [];

  // register the global timer
  var id = setInterval(
    function() {
      var idx;
      for (idx in callbacks) {
        callbacks[idx]();
      }
    }, freq);

  // return a Global Timer object
  return {
    "id": function() { return id; },
    "registerCallback": function(cb) {
      callbacks.push(cb);
    },
    "cancel": function() {
      if (id !== null) {
        clearInterval(id);
        id = null;
      }
    }
  };
}

var gt = makeGlobalTimer(500);
gt.registerCallback(function() {
                      console.log("a");
                    });

gt.registerCallback(function() {
                      console.log("b");
                    });

setTimeout(function() { gt.cancel(); }, 5000);
aekeus
Thanks a ton. This is exactly the kind of this I was looking for.Much appreciated!
Mike Beepo