Here's a class I developed to manage intervals. It's not a static or
singleton, but instead should be instantiated to a variable. Can manage
multiple events and eventTypes. Most of the explanation is in the comments,
but here's an (untested) example:
import.com.neoarchaic.util.delay;
Class MyClass {
private var delay:Delay;
private var delayMeId:String
function MyClass () {
delay = new Delay(this);
//a target object, should have ideally been a proper class
var target:Object = new Object();
target.callHim = function (arg1, arg2){
trace ("callHim " + arg1 + " " + arg2)
}
//Call a function in this object, and get an id
delayMeId = delay.doLater("callMe", 1000, "arg1", "arg2")
//Call a function in the target object, Don't bother with id..
delay.doLater(target, "callHim", 2000. "arg1", "arg2")
//Call a function repeatedly. Don't bother with id..
delay.doLoop("callMeAgain", 1000, "arg1", "arg2")
//Interrupt the loop after 5 seconds
delay.doLater("interruptMessage", 5000)
}
public function callMe(arg1, arg2){
trace ("callMe " + arg1 + " " + arg2)
}
public function callMeAgain(arg1, arg2){
trace ("callMeAgain " + arg1 + " " + arg2)
}
...
//To interrupt a specific message before execution using an id
private function interruptMe(){
delay.interrupt(delayMeId)
}
//To interrupt without an id, using the message instead
private function interruptMessage(){
delay.interrupt("callMeAgain")
}
//To interrupt everything
private function interruptAll(){
delay.interrupt("all")
}
}
And this is just some of what it can do.
Cheers,
Karina
/**
* Delay class - executes callbacks, broadcasts messages and events the
parent (or a passed target) after a set delay.
* delay.doLater() fires an event once and then clears itself.
delay.doLoop() fires the delay interval repeatedly until interrupted.
delay.doRepeat() executes the delay n number of times before clearing
itself.
* Each method is passed a variable number of parameters. The frist param is
an _optional_ target object that can be omitted when calling the parent
(passed to each Delay instance on creation). The second param is an event
string (ie, the name of the function to be called). The third param is the
delay. (when the target is omitted, the second and third params become first
and second).
* delay.doRepeat()
* Subsequent parameters are usually arguments that will be sent on to the
target with the message.
* The dispatchEvent and broadcastMessage functions are shotrcuts of
doLater(), designed to work with a com.neoarchaic.util.AbstractBroadcaster
implementation or can be used with other implementations such as
EventDispatcher and ASBroadcaster (the broadcastMessage method is a custom
implementation of AbstractBroadcaster). To work, these methods should be
present in the parent or target object.
* Each event (an object containing the message, type, target, etc.) is
stored in the events object and a unique id is returned to the parent. The
message execution can be interrupted & cleared at any time by calling the
interrupt() function, passing either the event id, message, event type, or
"all".
* Muliple events can be stored in one object, but with duplicate events
(same message, type and target), the old event will be interrupted and
cleared first.
*
* @author Karina Steffens
* @version 1.0
* @usage
* @class
*/
class com.neoarchaic.util.Delay {
public var parent:Object;
private var events:Object;
private var index:Number;
/**
* Instantiate the delay.
* A reference to the object that created the Delay instance is
stored as the parent property.
* @param aparent:Object
*/
function Delay (aParent:Object) {
parent = aParent;
index = 0;
events = new Object ();
}
/**Send the message to the parent after the delay - once.
* @param target Object
* @param evt String
* @param delay Number
*/
public function doLater ():String {
//target:Object, evt:String, delay:Number
//_global.tt ("doLater", arguments)
return storeMessage ("doLater", arguments);
}
/**Send the message to the parent after the delay - continuous.
* @param target Object
* @param evt String
* @param delay Number
*/
public function doLoop ():String {
return storeMessage ("doLoop", arguments);
}
/**Send the message to the parent after the delay - n times.
* @param target Object
* @param evt String
* @param delay Number
* @param times Number
*/
public function doRepeat ():String {
return storeMessage ("doRepeat", arguments);
}
/**Dispatch the event to listeners after the delay.
* @param target Object
* @param evt String
* @param delay Number
*/
public function dispatchEvent ():String {
return storeMessage ("dispatchEvent", arguments);
}
/**Broadcast the event to listeners after the delay.
* @param target Object
* @param evt String
* @param delay Number
*/
public function broadcastEvent ():String {
return storeMessage ("broadcastEvent", arguments);
}
/**Broadcast the message to listeners after the delay.
* @param target Object
* @param evt String
* @param delay Number
*/
public function broadcastMessage ():String {
return storeMessage ("broadcastMessage", arguments);
}
/**Interrupt and clear the event before it executes, or stop an
infinite interval loop.
* The interrupt id can be one of the following (from specific to
general):
* The id for a specidic event (returned to the parent when the event
was stored)
* The message string - interrupts all events with that message
string. (eg: delay.doLater("callMe", 1000); delay.interrupt("callMe");)
* The event type - interrupts all events with that type ("doLater",
"doRepeat", "doLoop", "dispatchEvent", "broadcastEvent" or
"broadcastMessage")
* "all" - interrupts all the currently stored events.
* @param id String
*/
public function interrupt (id:String) {
var myEvent:Object;
myEvent = events[id];
if (myEvent == undefined) {
for (var i in events) {
myEvent = myEvent = events[i];
if (id=="all" || myEvent.message == id ||
myEvent.evtType == id) {
clearInterval (myEvent.interval);
delete events[i];
}
}
}else{
clearInterval (myEvent.interval);
delete events[id];
}
}
//Store the message for later execution
private function storeMessage (evtType:String, args:Array):String {
//_global.tt ("storeMessage", evtType, args, typeof
(args[0]))
var myEvent:Object = new Object ();
if (typeof (args[0]) != "string") {
var target:Object = args.shift ();
myEvent.target = target;
} else {
myEvent.target = parent;
}
var evt:String = String (args.shift ());
var delay:Number = Number (args.shift ());
//generate a unique id for that event. Use evtType and evt
as part of the string for debugging
var id:String = evtType + "_" + evt + "_" + index++;
switch (evtType){
case "doRepeat":
myEvent.times = args.shift ();
break;
case "doLoop":
myEvent.times = "unlimited";
break;
default:
myEvent.times = 1;
}
if (evtType.indexOf ("do") == -1) {
//AbstractBroadcaster or EventDispatcher methods
myEvent.evt = evtType;
args.unshift (evt);
} else {
myEvent.evt = evt;
}
//store the original message seperately
myEvent.message = evt;
myEvent.id = id;
myEvent.evtType = evtType;
myEvent.args = args;
myEvent.delay = delay;
//clear previous events that are identical to this one (same
message, type and target)
for (var i:String in events){
var e:Object = events[i];
if (e.message == evt && e.evtType == evtType &&
e.target == myEvent.target){
clearInterval (e.interval);
delete events[i];
break;
}
}
events[id] = myEvent;
myEvent.interval = setInterval (this, "sendMessage", delay,
myEvent);
return id;
}
//Execute the event and clear it if required.
private function sendMessage (myEvent:Object) {
//_global.tt ("sendMessage", myEvent)
var evtType:String = myEvent.evtType;
var target = myEvent.target;
var evt:String = myEvent.evt;
var args:Array = myEvent.args;
target[evt].apply (target, args);
if (typeof (myEvent.times) == "number") {
myEvent.times--;
if (!myEvent.times) {
clearInterval (myEvent.interval);
delete events[myEvent.id];
}
}
}
}
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com