0

On my local server, everything works fine, but when I upload the scripts to my web server and run the script, I get a php - mysql error message: Too many connections.

I'm trying to insert a delay to $.post(); function but I don't know how.

var videoArrays = new Array();

videoArrays[0] = '34hgfj23rj';
videoArrays[1] = '2kj4mfwjmhkg';
videoArrays[2] = '23kjrmkf4ekjf';
...
videoArrays[152] = '3ok4jtmfj324hfh';

for(var i in videoArrays){
   $.post('get_data.php', { v_data : videoArray[i] },
   function(data){
      if(data == 'exists'){
         alert('Data exists in data base !');
      }
      else alert('Invalid data !');  
   });
}

I get the error, because I open / close many connections in less than 3 seconds ( 152 different connections ) !

I need to add a delay, but where or how... I don't know.

Reteras Remus
  • 923
  • 5
  • 19
  • 34
  • 3
    Probably stating the obvious, but why not keep the connection open, run all the code and close it afterwards, by posting the videoArrays in one request. – Paul Grimshaw Nov 02 '12 at 14:05
  • 1
    I basically agree with @PaulGrimshaw - you should have your API on the server accept a list of names, and it should return a map indicating which ones are or are not available. (Yes sorry I am still working on my first cup of coffee :-) – Pointy Nov 02 '12 at 14:07
  • I don't close the connection with mysql ( 'get_data.php' ), I only included the 'config.php' for connection. – Reteras Remus Nov 02 '12 at 14:07
  • I'm not sure how to do it with jQuery, but with regular javascript, you can attach a load event listener to your POST request. You could listen for a response then iterate on to the next request. 'ajaxrequest.addEventListener('load', handleload, false);' –  Nov 02 '12 at 14:10

1 Answers1

1

You need to create some sort of queue, so you can control how many requests are active at one time. An example of a queue is found here: How can I make batches of ajax requests in jQuery?

and here: Queue ajax requests using jQuery.queue()

and here: http://gnarf.net/2011/06/21/jquery-ajaxqueue/

Community
  • 1
  • 1
Jason M. Batchelor
  • 2,951
  • 16
  • 25