Timer Explanation

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!

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 :smile:

Some further boiling down:

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.)

1 Like

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?

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

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

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

appreciate the time :smile:

thank you