# Timer Explanation

**URL:** <https://forum.kirupa.com/t/timer-explanation/633693>\
**Category:** flash\
**Created:** [August 28, 2015, 7:42am UTC](https://forum.kirupa.com/t/timer-explanation/633693 "2015-08-28T07:42:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zamboolah\_Wigi\_Wigi](https://avatars.discourse-cdn.com/v4/letter/z/8dc957/32.png) [@Zamboolah\_Wigi\_Wigi](https://forum.kirupa.com/u/Zamboolah_Wigi_Wigi)\
**Post date:** [August 28, 2015, 7:42am UTC](https://forum.kirupa.com/t/timer-explanation/633693/1 "2015-08-28T07:42:49Z")

</div>

Hello,

I have limited knowledge to ActionScripts and wanted to have a timer that stops for a moment, then continues to play on the timeline. I found this that works but I really don’t understand the whole thing. All I do is replace some values and hope they don’t screw up along the way.

Can someone explain to me how this script works that I found in the web?

```
stop();
 
sec = 1;
fn = new Number(_root._currentframe);
 
_global.fwd = function() {
            fn = new Number(_root._currentframe);
            sec--;
            if (sec == 0) {
                        gotoAndPlay(fn + 1);
                        clearInterval(countDown);
                        sec = 1;
            }
}
 
countDown = setInterval(fwd, 1000); 

```

Thank you so much!

---

<div class="post-metadata">

**Author:** ![zeke\_chan](https://avatars.discourse-cdn.com/v4/letter/z/f14d63/32.png) [@zeke\_chan](https://forum.kirupa.com/u/zeke_chan)\
**Post date:** [August 28, 2015, 3:54pm UTC](https://forum.kirupa.com/t/timer-explanation/633693/2 "2015-08-28T15:54:38Z")

</div>

Hey there

Let’s start with the last line:

```
countDown = setInterval(fwd, 1000);

```

The setInterval is used to start a timer and there are 2 arguments here:

- fwd : which is the function that will be called once the timer has reached the preset time
- 1000 : which is the amount of time passed in miliseconds to call the fwd function. 1000 miliseconds is 1 second

The setInterval is assigned to the countDown variable so it can be used to clear or remove the timer. Which is what you get in the line:

```
clearInterval(countDown);

```

Back to the top:

```
sec = 1

```

This is used to keep track of the time left in seconds.

```
fn = new Number(_root._currentframe);

```

This is used to keep track of the current frame on the timeline where the playhead is. There is no need to convert it into a number as the \_currentframe value s already a number.

```
_global.fwd = function() {

```

The \_global keyword is used to ensure you can call the fwd function from anywhere in your Flash movie.

```
sec--;

```

This basically changes the current sec value by -1. It is the same as:

```
sec = sec - 1;

if (sec == 0) {

```

This checks to see if the value of sec has the value of 0. This line and the previous line is redundant because the setInterval only runs after 1 second.

```
gotoAndPlay(fn + 1);

```

Goes to the next frame in the timeline and continue playing.

There is a simpler way to write this script:

```
stop();
 
_global.fwd = function() {
     gotoAndPlay(_root._currentframe + 1);
     clearInterval(countDown);
}

countDown = setInterval(fwd, 1000);

```

Hope that helps 😄

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [August 29, 2015, 1:01am UTC](https://forum.kirupa.com/t/timer-explanation/633693/3 "2015-08-29T01:01:04Z")

</div>

Some further boiling down:

```javascript
stop()
setTimeout(gotoAndPlay, 1000, _currentframe + 1)

```

(I guess, depending on where the code was placed in the .fla, you might still need the `_root.`, but you often wouldn’t.)

---

<div class="post-metadata">

**Author:** ![Zamboolah\_Wigi\_Wigi](https://avatars.discourse-cdn.com/v4/letter/z/8dc957/32.png) [@Zamboolah\_Wigi\_Wigi](https://forum.kirupa.com/u/Zamboolah_Wigi_Wigi)\
**Post date:** [August 29, 2015, 12:32pm UTC](https://forum.kirupa.com/t/timer-explanation/633693/4 "2015-08-29T12:32:07Z")

</div>

Hi @krilnon zeke\_chan

appreciate the time. I’m not going to pretend I understand what you just wrote. hehehehehehe it’s a lot to swallow for a beginner like me.

I forgot to add this to my question which is my only concern:

changing the first code which is

```
sec = 1;

```

does the job right? I want it to pause for 1 second and play. but I also change the other set located after

```
 gotoAndPlay(fn + 1);
          clearInterval(countDown);
          sec = 1;

```

or is there no need to do that?

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [August 29, 2015, 5:53pm UTC](https://forum.kirupa.com/t/timer-explanation/633693/5 "2015-08-29T17:53:39Z")

</div>

The thing that controls how long to pause for is the number `1000`… but I’m not sure what you’re asking now.

---

<div class="post-metadata">

**Author:** ![Zamboolah\_Wigi\_Wigi](https://avatars.discourse-cdn.com/v4/letter/z/8dc957/32.png) [@Zamboolah\_Wigi\_Wigi](https://forum.kirupa.com/u/Zamboolah_Wigi_Wigi)\
**Post date:** [August 30, 2015, 5:26pm UTC](https://forum.kirupa.com/t/timer-explanation/633693/6 "2015-08-30T17:26:58Z")

</div>

Hi Krilnon,

What I’m currently doing right now is if I want to have a 3 second stop, I do this:

```
stop();
 
sec = 3;
fn = new Number(_root._currentframe);
 
_global.fwd = function() {
            fn = new Number(_root._currentframe);
            sec--;
            if (sec == 0) {
                        gotoAndPlay(fn + 1);
                        clearInterval(countDown);
                        sec = 3;
            }
}
 
countDown = setInterval(fwd, 1000);

```

notice I changed this one ----\> `sec = 1` to `sec = 3`

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [August 30, 2015, 7:39pm UTC](https://forum.kirupa.com/t/timer-explanation/633693/7 "2015-08-30T19:39:04Z")

</div>

Oh. In that case, you don’t need to change the second one.

---

<div class="post-metadata">

**Author:** ![Zamboolah\_Wigi\_Wigi](https://avatars.discourse-cdn.com/v4/letter/z/8dc957/32.png) [@Zamboolah\_Wigi\_Wigi](https://forum.kirupa.com/u/Zamboolah_Wigi_Wigi)\
**Post date:** [September 1, 2015, 12:56am UTC](https://forum.kirupa.com/t/timer-explanation/633693/8 "2015-09-01T00:56:50Z")

</div>

appreciate the time 😄

thank you
