interval manager class

OK, so you are tired of loosing your interval ID's, you always have to remember to kill any interval before setting it again and such.
Well, intervalManager will basically store all those precious id's under a list, to which you can always reference and clear anytime.

How does setInterval work?
Well, nobody really knows how (except of course the maestro engineers over at mm), but from what I've seen in practice, the interval ID that setInterval () returns seem to be a global array element number. Probably, somewhere up there there's an engine, that basically scans that array, and if there's an interval which should be fired at certain time, then it does so.

So what is the biggest mistake that all developers do when setting intervals?

intervalID = setInterval (functionName,interval,params) // if this is the first interval that we set in this movie, then // the value of that variable ( intervalID ) will be 1 : trace(intervalID) // returns: 1 // // later we set it again in exaclty the same way: intervalID = setInterval (functionName,interval,params)

Now what happened here?

The variable "intervalID" got overwritten with the new value returned by setInterval. This was the second interval that we set in the movie, hence it will return 2:

trace(intervalID) // now, it returns: 2

Most of us will think that what happened here is that we actually killed the first interval, and overwrote it with the new one.

*wrong*

Now we have 2 intervals running instead of one.

what happened then with our first intervalID?

well, I'm afraid to say it was lost the time we overwrote that variable. Now we basically lost that precious ID which was the only way we had to reference that interval later so that we can delete it.

So what's the solution to this?

well, the most common way of dealing with this would be to add a line clearing the interval, before we set the new one:

clearInterval (intervalID)
intervalID = setInterval (functionName,interval,params)

But of course that means always writing two lines of code when normally it should be one right?

So here is where intervalManager gets to the stage.

intervalManager.setInterval("intervalName",callBack,time)

basically intervalManager stores all interval ids under a globaly available list, and let's you reference them through unique strings that are asigned to each interval.

- "Enough! show me the code!"

ok ok ok, this is it:

get the .as file



/*===============================================================
IntervalManagerClass
by Marcos Weskamp

ver 0.0
last revised dec 17 2002

thanks to Matty Sallin and Dan Albritton for their tips

Usage example below

=================================================================

Open issues in this version:

1- it's not taking all the parameters a setInterval can take (check out the
reference panel, and there are some other optional parameters, of which I'm
not taking care of now)

status: not done

2- it's not solving the unloadMovie issue. - if a interval is set inside a
child movie, when that child movie gets unloaded, it won't kill or take
that interval with it.
this one could be somehow approached passing a reference of where that
interval was set, into a second list, and on unloading of a movieClip,

that movieClip should basically go to that list, and clear all intervals
that belongs to him.

status: not done

=================================================================*/

IntervalManagerClass = function () {
        this.list = {}
}

IntervalManagerClass.prototype.setInterval = function (name,functionName,interval,params) {
        this.clearInterval(name)
        this.list[name] = setInterval(functionName,interval,params)
}

IntervalManagerClass.prototype.clearInterval = function (name) {
        clearInterval (this.list[name])
        delete this.list[name]
}

IntervalManagerClass.prototype.clearAll = function () {
        for (var n in this.list){
                clearInterval(this.list[n])
        }
        this.list = {}
}

if (intervalManager == undefined )
_global.intervalManager = new IntervalManagerClass()



/*==============================================================
// :: USAGE EXAMPLE:
/*==============================================================

#include "intervalManager.as"


ref = {}
ref.mattyCallBack = function () {
        trace ("Matty Called")
}
ref.danCallBack = function () {
        trace ("Dan Called")
}



// set an Interval "dan" to execute every 5 ms
intervalManager.setInterval("dan",ref.danCallBack,5)
// clear interval "dan"
intervalManager.clearInterval("dan")
// set an Interval "dan" to execute every 1000 ms
intervalManager.setInterval("dan",ref.danCallBack,1000)

// set an Interval "matty" to execute every 5 ms
intervalManager.setInterval("matty",ref.mattyCallBack,5)
// overWrite "matty" to execute now every 1000 ms
intervalManager.setInterval("matty",ref.mattyCallBack,1000)

// clear all intervals in this movie
//intervalManager.clearAll()


get the .as file


all content © copyraito2002