7 Replies Latest reply on Jul 24, 2009 10:45 AM by Rothrock

    setInterval interrupted - won't clear



      I have an external swf that plays an intro in multiple frames of various actionscript fade ins and fade outs. I use a setInterval command on frames that I want to stay up for a period of time:


      function hold(){


      myInterval = setInterval(hold,2000);


      Everytime I use the setInterval, I clear it on the next frame




      While the site is running - if you interrupt the setInterval by clicking on another menu item it sems to carry over to all other sub-pages. So the website just plays continuously through with a setInterval of 2000. Even through frames that I know have the clearInterval command on them.

      Any help would be much appreciated!


        • 1. Re: setInterval interrupted - won't clear
          Rothrock Level 5

          When using setInterval you should never set it without first clearing it.



          myInterval = setInterval(hold,2000);


          setInterval is sort of like those numbers you get at a bakery or a butcher shop. So when you set your interval let's say it returns 8 and that is the value that is put in myInterval.


          So with normal navigation you get to the next frame an you clearInterval(myInterval); and the clearInterval asks the variable, "He what number you got?" and the myInterval variable says, "I got 8." And the interval is cleared.


          But when you have somebody click on some navigation that jumps around....


          This time myInterval got the ticket 68762. But we jump to a different frame and a new interval is set and given to myInterval and since a variable can only hold one value at a time it throws away that ticket and takes the new one, 68769. Now when it comes time to clear the interval it can only show the 68769 ticket. The previous one is lost and nobody knows what number it had -- except that it was lower than the latest ticket.


          So there are some tricks. You can make a loop that just clears all ticket numbers,


          for(var i=0;i<10000000000;i++){




          but of course that could clear things you don't want cleared yet. And it is wasteful of processort time.


          You could make an array that everytime you setInterval you push the ticket onto and then clear all of them:




          for(var a in myArray){




          Again you could have some pushed on there that you want cleared and some you don't and it does make things a bit more complicated.


          Or you could do what I suggested first. Always clear your interval before setting it. If there isn't one to clear no harm done and if there is you will prevent having having a "lost ticket."

          • 2. Re: setInterval interrupted - won't clear
            thesaunz Level 1

            Thanks Rothrock,

            I tried clearing the interval before every time I set it and it still doesn't work. Also tried the other options... the first one take too much processor.


            I'm wondering if it's because when it is interrupted by a button click it goes from one to another external SWF file. Maybe it can't figure out the value of the setInterval anymore, and therefore can't clear it? I have multiple SWF files with the same interval (myInterval) could this be the problem? I even tried putting a clearInterval on every frame of the timeline in the main SWF that all the external SWF's play from. Any ideas?


            again thanks for you help!

            • 3. Re: setInterval interrupted - won't clear
              Rothrock Level 5

              Yes it is most likely the case that by jumping from one external swf to another you are not having the proper scope to see the variable. You could add some trace statements to your code just before and after the clear-set thing.


              trace("pre clearing ticket value is: "+myInterval);



              trace("just set the interval and got ticket: "+myInterval);


              and similar where ever you set/clear.


              You aren't doing something silly like loading the external swfs into level zero and completely replacing the base player?


              In any event you now know enough to figure this out. There is nothing magic about intervals or the ticket number they return. You just have to not lose the ticket!


              PS: And just because the clear before set rule wasn't the answer to this specific problem...you should still always do it. Really, trust me on this one.

              1 person found this helpful
              • 4. Re: setInterval interrupted - won't clear
                thesaunz Level 1

                Thanks! I fixed it! or at least found something that works...


                Once I saw the ticket numbers (starting at 3 and going up by 1) I put the loop statement in with a more realistic number so as not to take up processing speed:


                for(var i=0;i<300;i++){






                Thanks again for all your help!

                • 5. Re: setInterval interrupted - won't clear
                  Rothrock Level 5

                  That is really a bad solution. I'm sorry that I taught it to you.


                  If your file runs for a little while you could easily go over 300. Additionally if you every use any adobe components, video player, even certain class methods, they too use the same roll of tickets for their internal code and you would be breaking those. I can't stress how bad an idea that is.


                  You should really work on storing the ticket in a safe place that everywhere can reach. Another approach would be add a clearInterval() to your navigation buttons. Or better yet define a clean up function somewhere safe and then have the navigation buttons call that.

                  • 6. Re: setInterval interrupted - won't clear
                    thesaunz Level 1



                    Can you give an example of a clean up function that would work for something like this? seems like it is only reacting to concrete numbers rather than the name of the setInterval.

                    • 7. Re: setInterval interrupted - won't clear
                      Rothrock Level 5

                      Over the year's experience has taught me how to set these things up so that they are easy and just work. But it kind of requires setting up a whole system of how you load and unload and stuf and such. So without know all the structure of what you are doing it is hard to give exact advice.


                      Did you try the traces? What did they show? I'm guessing that they showed undefined for the value of myInterval. In that case it is showing you that your scope isn't correct or that by using whatever external loading technique you are using that the variable is being destroyed. You could add something like this to the traces to help you see what is going on:


                      trace("pre clearing ticket number: "+myInterval+" in the current scope: "+this);


                      That will let you see if you are in the right bakery.


                      So you need to put the variable someplace that everybody can agree on and nobody with just outright destory the bakery and all the tickets in it!


                      I don't normally do this type of thing, but without a proper structure in place you might want to use _root.





                      Of course then you have to be really careful about always clearing it before setting it since you could have multiple places where it might get set.


                      Of course I don't think this will work if you are doing the silly thing of loading into level zero and completely replacing your player...