Interval Manager, Dev Kit

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

Yea, Flash’s interval management really does suck. This is really useful. Thanks for sharing.

It’s never a problem, I’m more than happy to share when possible. Take Care.

_Michael

Here’s an update…


/**
 * 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.5
 */
class createage.managers.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.
    * @usage <code>
      import createage.managers.IntervalManager;

      IntervalManager.setInterval("hello", this, "sayHello", 1000, ["Michael"]);

      function sayHello(args:Array) : Void
      {
	      trace("Hello, " + args[0]);
      }

      function onMouseDown()
      {
	      IntervalManager.clearInterval( "hello" );
      }
      
    </code>
    */
   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.
    *
    * @usage <code>
      import createage.managers.IntervalManager;

      IntervalManager.setTimeout("helloOnce", this, "sayHelloOnce", 1000);

      function sayHelloOnce() : Void
      {
      	trace( "Hello from the TimeOut" );
      }    
    </code>
    */
   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.
    *
    * @usage <code>
      import createage.managers.IntervalManager;

      IntervalManager.setInterval("hello", this, "sayHello", 1000, ["Michael"]);

      function sayHello(args:Array) : Void
      {
	      trace("Hello, " + args[0]);
      }

      function onMouseDown()
      {
	      IntervalManager.clearInterval( "hello" );
      }
      
    </code>
    */
   public static function clearInterval( name:String ) : Void
   {
      _global.clearInterval( IntervalManager.intervalIDs[ name ] );
      delete IntervalManager.intervalIDs[ name ];
   }

   /**
    * Clears the timeout with the name you provide.
    *
    * @param The name of the timeout you'd like to clear.
    *
    *@usage <code>
      import createage.managers.IntervalManager;

      IntervalManager.setTimeout("hello", this, "sayHello", 2000, ["Michael"]);

      function sayHello(args:Array) : Void
      {
	      trace("Hello, " + args[0]);
      }

      function onMouseDown()
      {
	      IntervalManager.clearTimeout( "hello" );
      }
    </code>
    *
    */
   public static function clearTimeout( name:String ) : Void
   {
      _global.clearTimeout( IntervalManager.intervalIDs[ name ] );
      delete IntervalManager.intervalIDs[ name ];
   }
}


This is really coming a long nicely. Your system works so nice.

Yeah, I have had this for a while, honestly I have at least 150 classes sitting in my classpath, most of which I use often. Right now I am going through them and deciding what is worthy to be put in the kit, and stuff like this which I find VERY useful and use it quite often, I decided I’d give them to you guys before the kit.

This manager has proved extremely useful in the past, and I hope that it proves useful to you guys. Take Care.

Thanks for the feedback Templarian.

_Michael

Yet again, u never cease to amaze me. Awsome work. Any eta on the kit though, am really looking forward to being able to use it. :smiley:

Keep up the good work. One day ill learn how to write classes, maybe tomorrow… hmm, who am i kidding :lol:

Thanks once again for all your awsome work.

The kit is coming out in versions, unfortunately I am a poor musician, I have to find a way to make money.

Their will be a free basic version.

Then a Dev Kit Pro…

The kit is under the name, CreateageLib

Although you have followed me through most of these classes, so PM me your email, or better yet an instant messenger (aim, yahoo or msn) and I will see what I can do.

Take Care.

_Michael

There have been a few posts recently of people having difficulties with setInterval when reloading movies into clips etc, your class eliminates these problems:-Nice work

That was an issue forgot to mention, thanks stringy!

_Michael

THIS FREAKING ROCKS Oh this should be a built in class I swear!!! you rule

^I second that.^

Great work buddy!:kommie:

I hate to burst your bubble but you aren’t going to make money selling AS2 classes. Not because they’re bad or good or anything like that, it’s just that you’d have to be a sucker to spend money on them when there is hundreds more on the web (or you could make it yourself) :slight_smile:

aww your releasing so many classes, maybe i should release my vector class, :open_mouth: like for the people that know other programming languages

Edit: it is a nice class too, when i first started using flash i never used intervals because they were so hard, maybe you will give some nubs some hope

I don’t plan on getting rich, and I’m not aiming at just the at home every day programmer. I know a few studios that are already interested, as well as companies that I do consulting work for. Companies don’t want to waste time making something that has already been made, if they know it works and it works well, they will spend the money and get it, I know this first hand.

Plus it’s not just AS 2.0 Classes, this is just the beginning and the focus. I have alot of Extensions and Components that I have written over the last 2 or 3 years, as well as server side scripts that compliment development and boost efficiency.

With that said, I don’t plan on getting rich, if I could pay for hosting that would be great :). Take Care.

_Michael

My point is why pay for something when you can get it for free? But, not having much experience on marketing stuff, I’ll take your word for it.

You might be able to work out a book deal with friendsofed or o’reilly using a collection of classes along with how and why to implement them. Something like “Efficient Actionscript” or “Time-Saving Actionscript” might get their attention.

Friends of Ed would never do that for me :frowning: lol

_Michael

I made an IntervalManager class a while ago too.

You can find it here:
http://dauntless.be/blog/?p=11

@Dauntless - Nice…

So I’ve been looking into Lindquist said, and it turns out, it’s not nearly as impossible as I was thinking it was going to be.

What are your guys’ opinions?

_Michael