Return to Snippet

Revision: 44104
at April 7, 2011 00:02 by scottwatkins


Updated Code
import flash.events.TimerEvent;
import flash.utils.Timer;

/**
 * delay function
 * a quick and easy delay function that can call a function with parameters. configurable
 * with delay time and repeat frequency
 *
 * @param func:Function The function to call when timer is complete
 * @param params:Array An array of parameters to pass to the function
 * @param delay:int [OPTIONAL] The number of milliseconds to wait before running the function
 * @param repeat:int [OPTIONAL] The number of times the function should repeat
 */
private function delay(func:Function, params:Array, delay:int = 350, repeat:int = 1):void
{
    var f:Function;
    var timer:Timer = new Timer(delay, repeat);
    timer.addEventListener(TimerEvent.TIMER, f = function():void
    {
        func.apply(null, params);
        if (timer.currentCount == repeat)
        {
           timer.removeEventListener(TimerEvent.TIMER, f);
           timer = null;
        }
    });
    timer.start();
}

Revision: 44103
at April 6, 2011 23:57 by scottwatkins


Updated Code
/**
 * delay function
 * a quick and easy delay function that can call a function with parameters. configurable
 * with delay time and repeat frequency
 *
 * @param func:Function The function to call when timer is complete
 * @param params:Array An array of parameters to pass to the function
 * @param delay:int [OPTIONAL] The number of milliseconds to wait before running the function
 * @param repeat:int [OPTIONAL] The number of times the function should repeat
 */
private function delay(func:Function, params:Array, delay:int = 350, repeat:int = 1):void
{
    var f:Function;
    var timer:Timer = new Timer(delay, repeat);
    timer.addEventListener(TimerEvent.TIMER, f = function():void
    {
        func.apply(null, params);
        if (timer.currentCount == repeat)
        {
           timer.removeEventListener(TimerEvent.TIMER, f);
           timer = null;
        }
    });
    timer.start();
}

Revision: 44102
at April 6, 2011 07:26 by scottwatkins


Updated Code
/**
 * delay function
 * a quick and easy delay function that can call a function with parameters. configurable
 * with delay time and repeat frequency
 *
 * @param func:Function The function to call when timer is complete
 * @param params:Array An array of parameters to pass to the function
 * @param delay:int [OPTIONAL] The number of milliseconds to wait before running the function
 * @param repeat:int [OPTIONAL] The number of times the function should repeat
 */
private function delay(func:Function, params:Array, delay:int = 350, repeat:int = 1):void
{
    var f:Function;
    var timer:Timer = new Timer(delay, repeat);
    timer.addEventListener(TimerEvent.TIMER, f = function():void
    {
        func.apply(null, params);
        timer.removeEventListener(TimerEvent.TIMER, f);
    });
    timer.start();
}

Revision: 44101
at April 6, 2011 07:26 by scottwatkins


Updated Code
/**
 * delay function
 * a quick and easy delay function that can call a function with parameters. configurable
 * with delay time and repeat frequency
 *
 * @param func:Function The function to call when timer is complete
 * @param params:Array An array of parameters to pass to the function
 * @param delay:int [OPTIONAL] The number of milliseconds to wait before running the function
 * @param repeat:int [OPTIONAL] The number of times the function should repeat
 */
private function delay(func:Function, params:Array, delay:int = 350, repeat:int = 1):void
{
    var f:Function;
    var timer:Timer = new Timer(delay, repeat);
    timer.addEventListener(TimerEvent.TIMER, f = function():void
    {
        func.apply(null, params);
        timer.removeEventListener(TimerEvent.TIMER, f);
        if (repeat == 1)
            timer = null;
    });
    timer.start();
}

Revision: 44100
at April 6, 2011 07:14 by scottwatkins


Initial Code
/**
 * delay function
 * a quick and easy delay function that can call a function with parameters. configurable
 * with delay time and repeat frequency
 *
 * @param func:Function The function to call when timer is complete
 * @param params:Array An array of parameters to pass to the function
 * @param delay:int [OPTIONAL] The number of milliseconds to wait before running the function
 * @param repeat:int [OPTIONAL] The number of times the function should repeat
 */
private function delay(func:Function, params:Array, delay:int = 350, repeat:int = 1):void
{
    var f:Function;
    var timer:Timer = new Timer(delay, repeat);
    timer.addEventListener(TimerEvent.TIMER, f = function():void
    {
        func.apply(null, params);
        timer.removeEventListener(TimerEvent.TIMER, f);
        timer = null;
    });
    timer.start();
}

Initial URL

                                

Initial Description
I've updated this snippet to remove the timer and null it after the function is repeated `repeat` times. You use it like this:  

    delay(functionname, [param1, param2], 350, 3);

This will call `functionname` three times with a 350 millisecond delay between each call and pass `param1` and `param2` to the function.

If you need to call a function that doesn't accept parameters, pass an empty array `[]` to the second argument.

Initial Title
ActionScript 3 Delay Function

Initial Tags

                                

Initial Language
ActionScript 3