Another little useful class I decided I’d give to you guys since it’s going to be a bit before the kit is out.
This is used to manage intervals. It may not seem like a big deal, but it has honestly saved me so much time and grief. It gives you one location for setting and clearing intervals, and even allows you to set a timeout function.
Here is the class, IntervalManager.as
/**
* Flash's interval management is horrible. Here we provide a way
* of setting and clearing intervals, in one location.
*
* @author Michael Avila
* @version 1.0.0
*/
class IntervalManager
{
// stores the interval ID's in a dictionary, so that you can name them
private static var intervalIDs : Object = new Object();
/**
* Sets an interval, storing it's reference in this IntervalManager. If there is already an interval associated with the
* name that you provide (meaning an interval that was created with but not cleared with the IntervalManager), the IntervalManager
* will clear the interval and set the interval you provide in it's place.
*
* @param The name you are assigning the interval, note that this is how you will be deleting it
* @param The path to the function that will be called by the setInterval
* @param The name of the function as a string
* @param The interval that this function will be called on
* @param The args that will be passed to the interval. The arguments are passed as an array, so be sure
* to have your function compensate for that.
*/
public static function setInterval( name:String, path:Object, functionName:String, interval:Number, args:Array ) : Void
{
if ( IntervalManager.intervalIDs[name] != undefined)
{
IntervalManager.clearInterval( name );
}
IntervalManager.intervalIDs[name] = _global.setInterval(path, functionName, interval, args);
}
/**
* Sets a function to be called at "interval" seconds after the timeout is set. If there is already a timeout associated with
* the name that you provide (meaning an interval that was created with but not cleared with the IntervalManager), the IntervalManager
* will clear the old timeout and set the new timeout you provide in it's place.
*
* @param The name you are assigning the timeout
* @param The path to the function that will be called by the setTimeout
* @param The name of the function as a string
* @param The interval that this function will be called on
* @param The args that will be passed to the timeout. The arguments are passed as an array, so be sure
* to have your function compensate for that.
*/
public static function setTimeout( name:String, path:Object, functionName:String, interval:Number, args:Array ) : Void
{
if ( IntervalManager.intervalIDs[name] != undefined)
{
IntervalManager.clearInterval( name );
}
IntervalManager.intervalIDs[name] = _global.setTimeout(path, functionName, interval, args);
}
/**
* Clears the interval with the name you provide.
*
* @param The name of the interval you'd like to clear.
*/
public static function clearInterval( name:String ) : Void
{
_global.clearInterval( IntervalManager.intervalIDs[name] );
delete IntervalManager.intervalIDs[name];
}
}
The code is simple… no need to worry about it.
Here is the code in the fla:
IntervalManager.setInterval("hello", this, "sayHello", 1000, ["Michael"]);
IntervalManager.setTimeout("helloOnce", this, "sayHelloOnce", 1000);
function sayHello(args:Array) : Void
{
trace("Hello, " + args[0]);
}
function sayHelloOnce() : Void
{
trace( "Hello from the TimeOut" );
}
function onMouseDown()
{
IntervalManager.clearInterval( "hello" );
}
This class will be found in the package,
createage.managers
in the Dev Kit.
The documentation for the dev kit can be found here…
www.createage.com/CreateageLib
Take Care.
_Michael