9 Replies Latest reply on Apr 10, 2013 8:00 AM by kglad

    setInterval too slow

    sjc77

      I am using flash to develop a behavioral experiment. The experiment is designed to have two people work on tasks together, simultaneously. In order to do this, a text file must frequently be called from a server. I use setInterval to do this as a way of keeping the players in sync.

       

      For instance, after the practice round, I give Player 2 a wait screen with no continue button. As soon as Player 1 has also finished the practice round and has clicked her continue button, the program sends a variable with value "Yes" to the external text file.  Player 2's program is constantly calling the text file on the server (using setInterval set to 1000ms) until it finds that specific variable with the value "Yes". At this point, the continue button is made visible on Player 2's screen.

       

       

      PROBLEM:

      setInterval does not seem to be consistently called. I experience erratic behavior: Sometimes it works immediately, sometimes it does not. When it does not work, I believe it is still calling setInterval, because if I leave the screen up, the continue button will eventually show up, even if it takes over 5 minutes to do so!!

       

      I tried sticking updateAfterEvent() in the function called by setInterval to make sure the stage was being updated after finding the variable, but this did not seem to solve the problem.

       

       

      CODE (for only one of the frames on which this is happening):

      stop();

       

      //////Continue button///////

      btnContinue.onRelease = function(){

       

                     //The following text will be read by first screen of Stage1and2_Boss before

                     //the continue buttone will appear.

           submitDataToTextfile("append",pairid+"_data.txt","workerfinishedpractice","Yes");

       

       

           gotoAndStop("Stage1and2_Worker",1);

      }

       

      //Make button invisible until the variable "practicefinished" takes on the value "Yes".

      btnContinue._visible = false;

       

      function endframe7(){

           myVars.load("URL_data.txt");

                     //"URL" is actually set to the web address of the server. I have removed it for this example.

           myVars.onLoad = function(success){

                if (success) {

                     if (myVars.practicefinished == "Yes"){

                          clearInterval(CheckInterval_Worker);

                          btnContinue._visible = true;

                          updateAfterEvent();

                     }

                }

            };

           updateAfterEvent();

      }

       

      CheckInterval_Worker = setInterval(endframe7,1000);

        • 1. Re: setInterval too slow
          kglad Adobe Community Professional & MVP

          1.   all timings in flash are approximate.  you can come pretty close to what you want IF the host computer is not taxed and you use self-correcting code and you use a frame rate that's relatively high.

          2.   file loads are asychronous and you have little control over the speed with which something loads.  that's mostly a network issue.

          3.   unless you're taking steps elsewhere (or, you're lucky), that text file is loading from the user's cache after the first load (and 2 is not an issue).

          4.   your setup is problematic.  using setInterval (especially with a frequency that high) with a load operation is a bad start that probably can't be made to work the way you want.

           

          you should be using server side code or localconnection to notify a user that the text file is ready for download, not a loop like setInterval.

          1 person found this helpful
          • 2. Re: setInterval too slow
            sjc77 Level 1

            Thanks for your reply.

             

            1. I don't know what self-correcting code is, but I do not have control over the host computer.

             

            2. I assume I can't do anything about this.

             

            3. Not sure what implications "loading from the user's cache" has.

             

            4. This point is likely the most important: Do you suggest another way of coding this? What is server-side code?

             

            I would appreciate it if you could refer me to some material that could help me out.

            • 3. Re: setInterval too slow
              kglad Adobe Community Professional & MVP

              use localconnection.

               

              that will allow each swf to send data to each other.  there may still be sync'ing issues because the communication will not be instant.

               

              i'm not sure what data you want to send but, for example, if you want the first person to finish the practice round to be presented with a wait signal and the second to be presented with a continue button, that would be something amenable you could do by having the wait signal being presented by default.  when it's presented, the swf would send a message to the other swf causing the other swf to not present the wait signal but present the continue button.  when the continue button is clicked that swf would continue and send a message (via localconnection) to the other swf that it should continue.

              1 person found this helpful
              • 4. Re: setInterval too slow
                sjc77 Level 1

                I think that sounds similar to what I am doing, except that I don't think I am doing it by "local connection." Here is what I am doing:

                 

                All my files are on a remote server in the same file, including the single .SWF folder. Both players access this same file. Each player accesses the experiment on a separate computer using a URL I provide them (the same one for everyone). They are linked up because I provide them both the same "pair ID" (e.g. 101) to type into an input box. This creates a text file on the server labeled "pairID_data.txt." This is how the two subjects' computers talk to each other during the experiment. Depending on the role each person is assigned, they are sent to different "scenes" in the .swf file.

                 

                Would localconnection allow communication between two browsers using the SAME swf file?

                 

                Message was edited by: sjc77

                • 5. Re: setInterval too slow
                  kglad Adobe Community Professional & MVP

                  you're not doing anything close.  your two swfs are both communicating with a text file via a server, not directly with each other.  the communication is swf1 to server repeatedly, swf2 to server repeatedly because you don't have any server side coding to push swf1's output to swf2.  that's very inefficent.  in a turn-based communication that can work but is still inefficient.

                   

                  localconnection establishes a swf to swf connection.  swf1 communicates directly to swf2.

                   

                  in any case, if you intend to have many users open your swf and you want to establish a pairing and connection between pairs, you should probably be using as3 and peer-to-peer communication using the netgroup class.

                  1 person found this helpful
                  • 6. Re: setInterval too slow
                    sjc77 Level 1

                    Oh, okay so you are saying that even though I have only one master copy of the swf, if two people are accessing it at the same time, there is technically one (temporary) swf created for each person?

                     

                    I will try using localconnection first.

                     

                    How difficult is it to convert to as3? does most of the same code work for as3?

                    • 7. Re: setInterval too slow
                      kglad Adobe Community Professional & MVP

                      some code converts easily from as2 to as3 (like math functions and for-loops).  some code needs more effort to convert.

                       

                      even though you're only creating one swf, your plan is to have more than one person open that swf at the same time and have those swfs communicate with each other.  localconnection is a workable option to communicate between two swfs.  but, if there might be 3 or more open swfs, localconnection won't be workable.

                       

                      for multiple open swfs you should consider as3 netgroup or using server side coding for some of the data handling.

                      1 person found this helpful
                      • 8. Re: setInterval too slow
                        sjc77 Level 1

                        I definitely plan to have more than two pairs accessing the swf at a time, so I will drop the localconnection idea.

                         

                        I know nothing about "server side coding." Would it be complex?

                        • 9. Re: setInterval too slow
                          kglad Adobe Community Professional & MVP

                          you would need to learn another language (php is probably the most common server-side scripting language and is similar to actionscript).  it would be most efficient to hand-off most of the coding to the server and have the swfs contain only the code to update their display.

                           

                          but, the minimum would be for the server to notify each client (swf) when a data update has been received (from one of the swfs) and to send those data to the clients.

                           

                          anyway, from what i know, complications abound in your project. 

                           

                          the biggest being:  you want clients to pair-up.  you'll need to take into account 1 user opening the swf and noone else.  then when a 2nd joins you'll want to pair them.  if one drops out, you'll need to re-pair the remaining one with someone else.  everytime there's a communication you'll need to check who its from (the paired person or not).

                           

                          i wrote a book that contains a chapter on multiplayer games and this is pretty similar in many ways.  in fact, you might want to check for information on creating multiplayer games and seeing the problems with keeping data secure (if that's a concern) and keeping the users sync'd.  pairing/matching players is still the biggest issue in non-realtime games.