Title: Timers not creating a delay
Tags: actionscript-3;actionscript;air
Question: I'm creating a program in Adobe Animate; one of functions is sending OSC messages to a DMX lighting program to change the lighting in the room.  

The standard changes are working as expected, but I'm having trouble with the "fades". I need to send a series of OSC messages in succession. 

What I have now is Adobe Animate creating series of timers through an independent function. What I feel I need is a delay feature, but I know this isn't possible in AS3. 

```function fadeFixtureData(fixture:int, rgbStart:Array, rgbEnd:Array, intervals:int):void
{
if (rgbStart.length != rgbEnd.length)
{
    return void;
}

var rgbCalculated:Array = new Array();

for (var i = 0; i <= intervals; i++)
{
    for (var j = 0; j < rgbStart.length; j++)
    {
        rgbCalculated[j] = ((((rgbEnd[j] - rgbStart[j])/intervals) * (i)) + rgbStart[j]);
    }

    delayedFunctionCall((i * 33), function(e:Event)         {sendFixtureData(fixture,rgbCalculated);});

    trace(i * 33);
    trace(rgbCalculated);

}


}

function delayedFunctionCall(delay:int, func:Function) {
var timer:Timer = new Timer(delay, 1);
timer.addEventListener(TimerEvent.TIMER, func);
timer.start();
}
```

The program seems to be tracing everything correctly, but the result is that all the messages are being sent at the same time. Only the last message is relayed to the lighting program.

Comment: You cannot create an anonymous dynamic function that you plan to be dependent on the for loop counter. This is not Javascript to allow such things, sorry.


Here is another answer: There are two ways I would suggest.  Both methods should let you interrupt any fade with a new one.  So halfway through a fade out, you might change your mind and want to fade on again (if this was for example, based on human interaction).

To achieve that, in these examples you just fire ```fadeTo(yourValue)``` again, as needed.

The EnterFrame approach:

```public class Main extends Sprite
{
    private var targetValue:Number;
    private var currentValue:Number = 0;
    private var increment:Number;
    private static const MAX_VALUE:int = 255;
    private static const FADE_TIME:Number = 5; // Seconds for a full fade from 0% to 100%.

    public function Main()
    {
        increment = MAX_VALUE / (stage.frameRate * FADE_TIME);  // Dynamically calculate based on app framerate.
        addEventListener(Event.ENTER_FRAME, enterFrameHandler);

        // Initiate a fade.
        fadeTo(1);
    }

    /**
     * Initiates fade.
     * @param   percentage A value between 0 and 1.  0 being off, 1 being full on, 0.5 as an example, being 50% brightness.
     */
    private function fadeTo(percentage:Number):void
    {
        if (percentage &gt; 1) percentage = 1;
        if (percentage < 0) percentage = 0;         

        targetValue = MAX_VALUE * percentage;

    }

    private function enterFrameHandler(e:Event):void
    {
        if (currentValue == targetValue) return; // No updates required.

        if (currentValue < targetValue)
        {
            currentValue+= increment;
            if (currentValue &gt; targetValue) currentValue = targetValue;
        } else {
            currentValue-= increment;
            if (currentValue < targetValue) currentValue = targetValue;
        }

        doRGBThing(currentValue);
    }

    private function doRGBThing(currentValue:Number):void
    {
        trace(int(currentValue));  // Replace this with your OSC related code.
    }
}
```

The Tween approach (like GreenSock's TweenLite):

```public class MainTween extends Sprite
{
    private var currentValueObj:Object = {currentValue: 0};
    private static const MAX_VALUE:int = 255;
    private static const FADE_TIME:Number = 5; // Seconds for a full fade from 0% to 100%.

    public function MainTween()
    {
        // Initiate a fade.
        fadeTo(1);
    }

    /**
     * Initiates fade.
     * @param   percentage A value between 0 and 1.  0 being off, 1 being full on, 0.5 as an example, being 50% brightness.
     */
    private function fadeTo(percentage:Number):void
    {
        if (percentage &gt; 1) percentage = 1;
        if (percentage < 0) percentage = 0; 

        TweenLite.killTweensOf(currentValueObj);
        TweenLite.to(currentValueObj as Object, FADE_TIME, {currentValue: MAX_VALUE * percentage, onUpdate: doRGBThing});
    }

    private function doRGBThing():void
    {
        trace(currentValueObj.currentValue);
    }
}
```


Here is another answer: You can use ```setTimeOut``` instead.

```var myTm = setTimeOut(delay, 1000); // in milliseconds

function delay(): void {
    // your delayed code
}
```


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:06/25/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<actionscript-3><actionscript><air>', 'Title': 'Timers not creating a delay', 'ViewCount': 1416, 'id': 56745615, 'user_AboutMe': '', 'user_AccountId': 9843700.0, 'user_DisplayName': 'Ben', 'user_Reputation': 11.0, 'user_Views': 56.0, 'user_id': 7291978.0}}