10 Replies Latest reply on Apr 11, 2007 8:33 AM by kglad

    using setInterval()

    GregConnTelevirtual
      My swf checks a file on a server every frame at the moment but that is a bit extreme, I'd like it to check it every 3 seconds. I found the setInterval() function but i'm having trouble getting it to work. I'm not really sure where abouts in the program it needs to be.

      I understand that this is the bit of code i need. But where does it go?

      setInterval(aFunction(),3000);

      I've tried putting it in the onEnterFrame() function with an if statement around it to control it but it is happening on every frame (probably as expected).....so yeah where do i put it or how do i call it etc.

      cheers

      Greg
        • 1. Re: using setInterval()
          kglad Adobe Community Professional & MVP
          attach it to a frame the plays when you want to start your every 3 second loop. you should assign an id to your setInterval() and use clearInterval() to ensure that it's never re-executed. under no circumstance would it make sense to put it in an onEnterFrame loop. and you should use the function name, not a function call in setInterval:

          • 2. Re: using setInterval()
            GregConnTelevirtual Level 1
            cheers for the reply kglad.

            I'm still not getting this, function that's being called within the set interval is actually in an external .as class file, so that's the only thing that happens which arent frames.......? If i put it in the main actionscript layer it only plays once....any ideas?

            • 3. Re: using setInterval()
              kglad Adobe Community Professional & MVP
              how would you call your function if you didn't use setInterval()?
              • 4. Re: using setInterval()
                GregConnTelevirtual Level 1
                I just call it directly in the onEnterframe() function as well as calling it when buttons are pressed but parsing through different parameters. It works as it is but it's just too busy, calling every frame hence wanting to call it in set frames, i mean i can do a frame count and do it but if there is a built in function that would be more efficient.
                • 5. Re: using setInterval()
                  kglad Adobe Community Professional & MVP
                  i meant, what code do you use.

                  and, are you trying to use setInterval() in your class file? if so, you must use setInterval(referenceObject,"aFunction",3000).
                  • 6. Re: using setInterval()
                    GregConnTelevirtual Level 1
                    Hi Kglad,

                    thanks again for the reply sorry for the late response, away from the office for the bank holiday period!

                    I'm not specifically trying to use setInterval() in the class file, I'm happy to use it anywhere i just need it to work really. Escuse the (possible) stupid question but what do you mean by 'referenceObject'?

                    cheers

                    Greg
                    • 7. Re: using setInterval()
                      kglad Adobe Community Professional & MVP
                      remove setInterval() from your onEnterFrame function. as i mentioned above, there's no circumstance where that would be worthwhile and most likely is going to cause your swf to stop responding.

                      there are two forms of setInterval(). the easiest form to use (and the one you are using) does not specify the scope of the function that's being called. and that can be a problem. in a class file, failing to specify scope is always a problem.

                      so, you would use the 2nd form of setInterval() that explicity transmits the scope of the function being called. referenceObject is the scope in the 2nd form.

                      i suspect you are going to be confused by this explaination and unable to use the information to solve your problem (unless removing from onEnterFrame is the solution).

                      so, if you still need help, post your code.
                      • 8. Re: using setInterval()
                        GregConnTelevirtual Level 1
                        I already removed setInterval from onEnterFrame, i did that when it didnt work right at the beginning of all this. When i said this stuff
                        quote:

                        Originally posted by: GregConnTelevirtual
                        I just call it directly in the onEnterframe() function as well as calling it when buttons are pressed but parsing through different parameters. It works as it is but it's just too busy, calling every frame hence wanting to call it in set frames, i mean i can do a frame count and do it but if there is a built in function that would be more efficient.


                        I was calling the function 'aFunction()' directly in onEnterFrame not setInterval. I will try this second form of setInterval() by specifying the scope and let you know how i get on.

                        cheers again

                        Greg

                        • 9. Re: using setInterval()
                          GregConnTelevirtual Level 1
                          cheers kglad, i finally go it to work. I found a working example and that helped me understand what the hell was going on, cheers for all your help!

                          Greg
                          • 10. Re: using setInterval()
                            kglad Adobe Community Professional & MVP
                            you're welcome.