Skip to main content
Participating Frequently
April 9, 2013
Answered

setInterval too slow

  • April 9, 2013
  • 1 reply
  • 1359 views

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);

This topic has been closed for replies.
Correct answer kglad

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?


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.

1 reply

kglad
Community Expert
Community Expert
April 9, 2013

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.

sjc77Author
Participating Frequently
April 9, 2013

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.

kglad
Community Expert
Community Expert
April 9, 2013

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.