Skip to main content
Participant
April 6, 2013
Question

How do I get the Interval to keep from repeating after it's used once?

  • April 6, 2013
  • 2 replies
  • 672 views

How do I get the Interval to keep from repeating after it's used once? Because I tried clearInterval but I couldn't get it working.

When my code looks like this, with ClearInterval, it refuses to execute my action, which is to change scenes after a short interval:

(Pardon if there is some formating errors, I had to reformat it for the Internet)

Code with clearInterval command

stop(); 

Kollektiv.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlayFromFrame_2);

function
fl_ClickToGoToAndPlayFromFrame_2(event:MouseEvent😞void{

  
gotoAndPlay(29);   
   var
myInterval:uint = setInterval(fl_ClickToGoToNextScene_14,3000);   
  
clearInterval(myInterval);
}

function
fl_ClickToGoToScene_14(event:MouseEvent😞void{
   
MovieClip(this.root).gotoAndPlay(1, "Testside");

However, when my code looks like this it refuses to stop looping:

Code without ClearInterval command

stop();

Kollektiv.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlayFromFrame_2);

   function
fl_ClickToGoToAndPlayFromFrame_2(event:MouseEvent😞void{

  
gotoAndPlay(29);
  
setInterval(fl_ClickToGoToNextScene_14,3000);

}
function
fl_ClickToGoToScene_14(event:MouseEvent😞void{
   
MovieClip(this.root).gotoAndPlay(1, "Testside");

Any ideas? Also, feel free to dumb your answer down, because I am completely new to Flash, Actionscript and coding.
This topic has been closed for replies.

2 replies

Inspiring
April 6, 2013

If you insist on using setInterval - you need to declare interval id in a higher scope so that you can clear it later:

stop();

var myInterval:uint;

Kollektiv.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlayFromFrame_2);

function fl_ClickToGoToAndPlayFromFrame_2(event:MouseEvent):void

{

          gotoAndPlay(29);

          myInterval = setInterval(fl_ClickToGoToNextScene_14, 3000);

}

function fl_ClickToGoToScene_14():void

{

          clearInterval(myInterval);

          MovieClip(this.root).gotoAndPlay(1, "Testside");

}

I suggest you use Timer instead:

stop();

import flash.events.TimerEvent;

import flash.utils.Timer;

var timer:Timer = new Timer(3000, 1);

timer.addEventListener(TimerEvent.TIMER, fl_ClickToGoToScene_14);

function fl_ClickToGoToAndPlayFromFrame_2(event:MouseEvent):void

{

          timer.start();

          gotoAndPlay(29);

}

function fl_ClickToGoToScene_14(event:TimerEvent):void

{

          timer.stop();

          MovieClip(this.root).gotoAndPlay(1, "Testside");

}

Participant
April 6, 2013

I will use a timer, thank you!

I used your timer code, but now the clip/scene won't play at all. I don't get any errors in the compiler though.

I know I should probably learn Actionscript properly instead of copying codes, but with a deadline coming up real fast I really don't have time.

EDIT: Figured it out. I just had to add

Kollektiv.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlayFromFrame_2);

after the timer function. Thanks guys.

Inspiring
April 6, 2013

If you only want to execute your function once, use either setTimeout

or use a Timer

Timers are in most cases a more precise and flexible solution.