# How to call a function dynamically?

**URL:** <https://forum.kirupa.com/t/how-to-call-a-function-dynamically/324635>\
**Category:** flash\
**Created:** [August 31, 2011, 6:39am UTC](https://forum.kirupa.com/t/how-to-call-a-function-dynamically/324635 "2011-08-31T06:39:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![tpann](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@tpann](https://forum.kirupa.com/u/tpann)\
**Post date:** [August 31, 2011, 6:39am UTC](https://forum.kirupa.com/t/how-to-call-a-function-dynamically/324635/1 "2011-08-31T06:39:44Z")

</div>

Say I have this:

```php

function f1():void {
     if (!paused) {
          // animate something
          // start timer and when it's done execute f2()
     } else {
          ??
     }
}
function f2(te:TimerEvent):void {
     if (!paused) {
          // animate something
          // start timer and when it's done execute f3()
     } else {
          ??
     }
}
function f3(me:TimerEvent):void {
     if (!paused) {
          // animate something then execute f4()
     } else {
          ??
     }
}
function f4():void {
     if (!paused) {
          // animate something
     } else {
          ??
     }
}

```

This series of functions happen serially as can be seen. I have a pause button that, when clicked, sets “paused” to true. This interrupts the flow of the animations, which is what I want to happen.

My problem is, how do I restart the animations when I unpause? I assume this would happen where the ??'s are.

I guess I could set up an Enter Frame listener that constantly checks the value of paused, but I would prefer a more, well, elegant solution.

Is there a way for me to define a variable:

```php
var currentFunction:Function = new Function();

```

…that I can set, in the ‘else’, to the function it’s in, like this?

```php
function f4():void {
     if (!paused) {
          // animate something
     } else {
          // somehow set currentFunction to f4();
     }
}

```

And then, in the function that sets paused to true or false, execute that function, like this:

```php
function setPause(me:MouseEvent) { // click a pause button
     if (!paused) {
          paused = true;
     } else {
          paused = false;
          currentFunction();
     }
}

```

Then too, for the functions passed a TimerEvent object, would I define a second function like this?

```php
var currentFunctionTimer:Function = new Function(te:TimerEvent);

```

I was thinking that instead of this I could dispatch a new TimerEvent but there are multiple functions that get passed a TimerEvent object so it doesn’t seem like that would work.

Thanks!
