Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Apr 06, 2013 Apr 06, 2013

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.
TOPICS
ActionScript
660
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Apr 06, 2013 Apr 06, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 06, 2013 Apr 06, 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");

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 06, 2013 Apr 06, 2013
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines