views:

3711

answers:

7

Hi,

I am creating a website that has an intro splash page that waits for 5 seconds before automatically sending the playhead to frame 17 if nobody has clicked the enter button doing the same thing.

My code for this is here:

function wait() { stop(); var myInterval = setInterval(function () { _level0.menu_number2 = 0; gotoAndStop(17); clearInterval(myInterval); }, 5*1000); // stop for 5 seconds } wait();

This all works fine but if I haven't waited and am in the site before 5 seconds is up I suddenly get taken back to frame 17 unintentionally after 5 seconds.

Now I know I need something in my code structure to check to see if the conditions have changed during these five seconds i.e the play head has moved beyond frame 15 and if so not to do anything but I am not sure what this something is.

please help.

+1  A: 

Try this code: it uses something called a closure (look it up if you're interested)

function wait(){
    stop();
    var myInterval = setInterval(function(){
        _level0.menu_number2 = 0;
        gotoAndStop(17);
        clearInterval(myInterval);
    }, 5*1000); // stop for 5 seconds
    return function(){ clearInterval(myInterval); };
}
var abortFunction = wait(); // Calls wait(), interval starts ticking
abortFunction(); // Aborts the interval

You could also stick a global variable somewhere and check for it, but that just pollutes the global namespace.

zenazn
A: 

You could always just clearInterval(myInterval) if they click the skip button. Or wrap it in a function unnecessarily: function stopwait(){ clearInterval(myInterval); };

To check the frame numbers you could use: if(_root._currentFrame < 17)

Eric Muyser
The problem here is that the reference to myInterval dies after wait() is done. That's what the closure is for. Yes--it would probably be cleaner not to have the wait() function at all (and just have a bunch of statements on a frame), but that's not what the question was asking.
zenazn
A: 

From Flash Player 8 onwards there is an intrinsic function called setTimeOut that you can use to implement a pause. I assume internally it's a wrapper around setInterval etc. It works like this:

function onContinue( message )
{
   trace("continue saying: " + message);
}

setTimeOut( onContinue, 1000, "hello" );

The first parameter is the function to call. The second parameter is the time to wait in milliseconds (1000 = 1 second) and the third parameter is optional and can be used to pass a value to the call back.

Luke
A: 

why not use setTimeout?

back2dos
A: 

Zenazn your code worked with a simple copy and paste so thanks for your help on this one, saved my skin ;-).

I didn't try any of the other solutions but thanks to those peeps for their help anyway.

A: 

Hi peeps,

Thought I should update this issue as I was a bit premature before.

After some testing I found Zenazn's code needed a little extra so I added the _currentFrame line from Eric Muyser and it all works fine now so thanks again to all.

Here is the Actionscript:

function wait(){
    stop();
    var myInterval = setInterval(function(){
      if(_currentFrame < 15){//This statement checks to see if the play head has moved past frame 15
      gotoAndStop(17);
    }
        clearInterval(myInterval);
    }, 5*1000); // stop for 5 seconds
    return function(){ clearInterval(myInterval); };
}
var abortFunction = wait(); // Calls wait(), interval starts ticking
What an odd and hard coded solution is this. Well, if it works for you...
Luke
A: 

you could use the tweenlite library http://blog.greensock.com/tweenliteas2/

TweenLite.delayedCall(delay:Number, onComplete:Function)

then if someone skips this you can call

TweenLite.killDelayedCallsTo(function:Function)

which kills the original delayedCall.

very easy and powerful.

Hope that helps, Josh

Josh