Archive for March, 2007

Procrastinator Class

Saturday, March 24th, 2007

Today I am debugging a video player project at work. One of the solution that I use in the debugging process is adding a delay to a function call. In the project I am using onEnterFrame function to create a counter delay and then execute the function at the end of the counter limit.
I think it will be more useful if I can create a static class function that can be reusable easier for other project.

I did some Google search about how other people doing delay in action script and come across to one sample here Delaying Actions – ActionScript.org but his method is for flash 4 and not enclosed in a portable / reusable class. So I created one static class like this:

/**
 *     utility to set a delay of action
 *
 * @author         OQ Ludiro
 * @date            Mar 24, 2007
 *
 * @class         Procastinator
 */

 
class com.ludiro.Procastinator
{
    private var __target_object : Object;
    private var __target_function : Function;
    private var __target_parameters : Array;
    private var __interval_id : Number;
    private var __done_flag : Boolean;
   
   
    /**
     * Procrastinator Constructor
     *
     * @param delay_timer : Number delay timer in milli seconds
     * @param targetScope : Object
     * @param targetFunction : Function
     */
    public function Procrastinator( delay_timer : Number , target_scope : Object, target_function : Function )
    {
        __done_flag = false;
        __target_object = target_scope;
        __target_function = target_function;
       
        clearInterval( __interval_id );
        __target_parameters = new Array();
        for( var i : Number = 3; i < arguments.length; i++ )
        {
            __target_parameters[ i - 3 ] = arguments[ i ];
        }

        // delay timer is in milliseconds
        __interval_id = setInterval( this, "runOnce", delay_timer );
       
    }
   
    /**
     * @function runOnce
     * @description run the function only once after certain delay time
     * @param   
     * @return    Void
     */
    private function runOnce(  ) : Void
    {
        trace( "\tProcrastinator:*runOnce invoked" );
        if( __done_flag )
        {
            var aActualParameters : Array = arguments.concat( __target_parameters );                
            __target_function.apply( __target_object, aActualParameters );
            clearInterval( __interval_id );                     
        }
        else
        {
            __done_flag = true;
        }       
    }

	/**
	 * @function cancel
	 * @description cancel the timer and clear the object
	 * @param	
	 * @return	Void
	 */
	private function cancle(  ) : Void 
	{
		trace( "\tProcrastinator:*cancle invoked" );
		clearInterval( __interval_id );
		delete __target_parameters;
		delete __target_function;
		delete __target_object;
		
		delete this;
	}

}// end of Class

Usage :

var delay_value : Number = 3000; // delay timer in milliseconds
var target_class : Object = MainClass; // this is the class where function resides
var target_function : Function = MainClass.functionCall;

var procast : Procrastinator = new Procastinator( delay_value, target_class, target_function );

Hope this helps in DELAYING / Procrastinating your Action 🙂