ethernet webserver example, client.stop causes lag time issues

Using the ethernet WebServer example code, I’ve got my arduino to host a website stored on an SD card. The website uses jquery to post the position of the mouse in the webbrowser back to the arduino. I would ultimately like to use this info to control a servo motor, however, the problem I have is that the client.stop line in each iteration of the void loop causes a large lag time in between when the mouse moves and when the arduino gets the information.

Is there a way to make this code only use the execute the stop.client line when the mouse stops moving. So effectively when there is no information being sent to the arduino via the post method?

This is the part I added to receive the mouse position and print it to serial. I would ideally like this to somehow loop without casing the client.stop line to execute (if possible without causing the site not to run)

                      String POST = "";
                      
                      while(client.available()){
                          c = client.read();
                          // save the variables somewhere
                          POST += c;
                      }
                      
                      if(POST != ""){
                              Serial.println(POST);
                      }

Here is my full code.

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>

String POST = "";
int count = 0;
const int chipSelect = 4;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,178,30);

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);
EthernetClient client;

void setup() {

      // Open serial communications and wait for port to open:
      Serial.begin(9600);
      Serial.setTimeout(10);

      //start the Ethernet connection and the server:
      Ethernet.begin(mac, ip);
      client.setTimeout(1); 
  
      server.begin();
      Serial.print("server is at ");
      Serial.println(Ethernet.localIP());
  
      //SD card stuff
      Serial.print("Initializing SD card...");
      // see if the card is present and can be initialized:
      if (!SD.begin(chipSelect)) {
          Serial.println("Card failed, or not present");
          // don't do anything more:
          return;
      }
      Serial.println("card initialized.");  
}

void loop() {
      // listen for incoming clients
      EthernetClient client = server.available();
      if (client) {
          Serial.println("new client");
    
          // an http request ends with a blank line
          boolean currentLineIsBlank = true;
          while (client.connected()) {
              if (client.available()) {
                  char c = client.read();
        
                  // if you've gotten to the end of the line (received a newline
                  // character) and the line is blank, the http request has ended,
                  // so you can send a reply
                  if (c == '\n' && currentLineIsBlank) {
                  // send a standard http response header
                      
                      String POST = "";
                      
                      while(client.available()){
                          c = client.read();
                          // save the variables somewhere
                          POST += c;
                      }
                      
                      if(POST != ""){
                              Serial.println(POST);
                      }

                      //load html/css/js for website only once
                      if (count <= 0){
                        
                          client.println("HTTP/1.1 200 OK");
                          client.println("Content-Type: text/html");
                          client.println("Connection: close");  // the connection will be closed after completion of the response
	                  //client.println("Refresh: 5");  // refresh the page automatically every 5 sec
                          client.println();
                      
                          File dataFile = SD.open("site.txt");
                          // if the file is available, write to it:
                          if (dataFile) {
                              while (dataFile.available()) {
                    
                                  //Serial.write(dataFile.read());
                                  client.write(dataFile.read());
                              }
                              dataFile.close();
                          }  
                          // if the file isn't open, pop up an error:
                          else {
                              Serial.println("error opening site.txt");
                          }
                      } 
          
                      //count = 1;
                      break;
               
                  }
              
                  if (c == '\n') {
                      // you're starting a new line
                      currentLineIsBlank = true;
                  } 
                  else if (c != '\r') {
                      // you've gotten a character on the current line
                      currentLineIsBlank = false;
                  }
              }
          }
          // give the web browser time to receive the data
          
          if (count == 0){
            delay(500);
          }
          else{
            delay(1);
          }
          count=1;
          
          // close the connection:
          client.stop();
          Serial.println("client disonnected");
      } 
}

I haven't tried using jQuery with Arduino, but perhaps you could leave the connection open until the (jQuery) client closes it?

thanks! sounds like a good idea actually!