A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Interval code duplicate error

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    9

    Question Interval code duplicate error

    Hello again This time i have a problem with intervals.

    I putted this:

    function wait()
    {
    stop ();
    var myInterval = setInterval(function ()
    {
    play ();
    clearInterval(myInterval);
    }, 5000);
    } // End of the function
    wait();

    To the 1st frame and it works. Stops for a few seconds and plays. Then i putted this to the 2nd frame to make it stop too. It appears a duplicate error. How make it works then?

  2. #2
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    If you put it on Frame 2, then you can't give your functions the same names, that's where the duplicate error is coming from. If you just want to stop it on Frame 2, use this:

    Actionscript Code:
    clearInterval(myInterval);

    but for to work, omit/remove var before the declaration of myInterval:

    Code:
    var myInterval = setInterval(function ()
    Remove that line, otherwise the interval will be created locally inside the function wait, and then you won't have access to it from anywhere else, for instance the _root level, which you are trying to on Frame 2
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    Thanks for the help it's working now. Didn't know that i can just change the function name. I do not want to create another thread so i'll ask here.

    Now that all my frames has different intervals (slide show) i want make a "skip" button. What i mean is: click to stop interval. So the slide show stops there but i not mean pause, i mean stop permanently the function with a click.
    Last edited by Soyal; 04-19-2012 at 07:15 AM.

  4. #4
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Stop the function, permanently with a click? What is that supposed to mean? The function is only executed when you call it, and in this case, it's with setInterval, and you can whenever you want, just clear that interval, as for the function, yes, you can delete it, but once you enter that Frame again, the Frame's codes will be re-executed, making the function re-created.
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    I explain. There is 10 frames in the project. Frame 1 and 2 has intervals. I want to make a jump to the 8th frame. So in 1st frame i added "Click to Go to Frame and Stop" code and i set there 8th frame. After i click the screen it jumps to 8th frame and after a while it moves to 9th (1st frame interval still works?). I want it to stay at 8th.

    I tried put stop at 8th but no effect.
    Last edited by Soyal; 04-19-2012 at 05:33 PM.

  6. #6
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    On the button or code, or even on Frame 8, just clear the interval

    It's not the function's fault, it's the interval which is not being killed properly
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  7. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    Whan i put clearInterval(myInterval); at 8th frame the error appears: 1120: Access of undefined property myInterval.
    Last edited by Soyal; 04-20-2012 at 07:38 PM.

  8. #8
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Oh, you're coding in Actionscript 3.0, why didn't you say so?

    In that case, you're probably using this, with var, inside the wait function, right?

    Code:
    var myInterval = setInterval(function ()
    Then you declare a local variable which only exists inside the function wait, and is not accessible from the outside. To solve this, declare the interval outside the function, 'cause then, you can clear it from another Frame:

    Actionscript Code:
    var myInterval;

    function wait()
    {
        stop ();
        myInterval = setInterval(function ()
        {
            play();
            clearInterval(myInterval);
        }, 5000);
    }
    wait();
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  9. #9
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    It works Thanks for the help and patience.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center