Reteras Remus
Reteras Remus

Reputation: 933

Javascript - check data one by one - Jquery post()

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.

Upvotes: 0

Views: 117

Answers (1)

Jason M. Batchelor
Jason M. Batchelor

Reputation: 2951

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/

Upvotes: 1

Related Questions