JavaScript Tick Paradigm

Recently I was playing with Ionic brushing up on my mobile js / ng skills.
My goal was a simple text game inspired by -
In my journey, I quickly discovered the need for a:

Tick-Based System - where game time progresses according to real time, but in discrete steps.

My first implementation resulted in quite a few messy intervals running in tandem, each looking something like this:

var combatTick  =   1000,
    combatInt   =   setInterval(function(){
         * Example Actions:
         * regenerateHealth();
         * applyDamageOverTime();
    }, combatTick);

The Problems

  • Hardcoded & Felt Clunky
  • Required multiple interval management if new actions became available later
  • No intuitive way to modify actions

So I scrapped this model in favor of something more like a queue system.

function Clock(){
    var actions = [], tickTime = 1000, tickCount = 0,
        i = setInterval(function(){
            tickCount++; // increment the count
            for (var x=0; x < actions.length; x++) {
                if (actions[x] && typeof actions[x] === "function") {
                } // perform the queued repeat actions
        }, tickTime);
    return {
        addAction: function(a){
        clearActions: function(){
            actions = [];
        getCount: function(){
            return tickCount;

In a nutshell, this factory method returns a Clock Object with private properties for
actionsList, tickTime and tickCount. As well as a private property i to hold the interval object. This clock object also have three public methods addAction,
clearActions and getCount.

Plans for the future

I think we could even improve upon this method going forward by creating an "Action Class" with
attributes to determine if this specific Action is "enabled", as well as setting its own frequency,
and number of times to repeat. Then we can have the Clock Manage all of our intervals for us.