1

I am having issues with my php server (my computer is the only connection). I initially thought part of the reason was because of too many ajax requests (I have a script that executes an ajax request per keystroke) so I implemented a design to control the flow of ajax requests into a queue. Below is my code:

//global vars:
activeAjaxThread = 0; //var describing ajax thread state
ajaxQue = [];  //array of ajax request objects in queue to be fired after the completion of the previous request

function queRequest(ajaxObj) {
  ajaxQue.push(ajaxObj);
  if (activeAjaxThread == 0) {
    fireOffAjaxQue();   
  } else {
    return 'ajax thread is running';
  }
}

function fireOffAjaxQue () {
  activeAjaxThread = 1;
  //getLastRequest();
  if ((ajaxQue.length > 0) && activeAjaxThread == 1) {
    $.ajax(ajaxQue[0]).always( function () {
      ajaxQue.shift(); 
      if (ajaxQue.length > 0) {
        fireOffAjaxQue();   //fire off another ajax request since this one has been completed. 
      }
    });
  }
  activeAjaxThread = 0;   //thread has finished executing
}

Implementation:

//create ajax object
var ajaxObj = {
  url: 'someplace.php',
  data: dataVar,
  success: function (data) {...}
};
//send ajax object to que
queRequest(ajaxObj);

Then I found out that Javascript is multi-threaded and read a few articles on Javascript's event handling, such as this on http://ejohn.org/blog/how-javascript-timers-work/ by John Resig (jQuery extra-ordinaire)

That being the case, wouldn't the functions I've presented here produce no results since Javascript is already queuing my requests? The weird thing is that it appears to change it drastically. My server crashes less, (not that that deems it a solution in any stretch of the imagination... argh), and after it crashes and restarts, the previous queued ajax requests get sent, when earlier they seemed to all be sent at once and disappear into thin air on server crash.

If Javascript, being single-threaded, queues asychronous events:

  1. Is there any point to having an Ajax request manager, or a queue?
  2. Why does my code produce any results at all?
JAAulde
  • 19,250
  • 5
  • 52
  • 63
Klik
  • 1,757
  • 1
  • 21
  • 38

1 Answers1

4

Javascript is single threaded, but once the AJAX request is sent, it is out of javascript's hands, so to speak. That's the Asynchronous part of AJAX. The request is made and then the code moves on. Then when the response is received it is processed. Requests are not queued.

James Montagne
  • 77,516
  • 14
  • 110
  • 130
  • Oh I get it... So the difference is that, by doing it this way, the request will fire after the last one has returned. Awesome... thanks for clearing that up :P. Such a long question and such a simple answer :P. – Klik Feb 12 '13 at 21:04
  • @TheWeirdNerd Also check out this answer: http://stackoverflow.com/questions/6903318/multiple-ajax-requests-delay-each-other/6933294#6933294 -- your asynchronous requests may wind up being effectively synchronous, and that might be contributing to the problems you're seeing server-side. It also may contribute to why your script has an effect -- it ends up delaying requests long enough to mitigate the issue I'm discussing in that answer. – Chris Baker Feb 12 '13 at 21:05
  • @Chris thanks for the link, but my issue isn't with sessions, I'm not using sessions. The Apache just tends to randomly crash siting the php5.dll for some reason and I notice it happens during Ajax requests. I can't figure out what's going on, no error code, and no data returned from the server. I checked my php code and seems to be in order. I think I need to write a separate question for that. It's a real pain in the knee. – Klik Feb 12 '13 at 21:11
  • Eh, too bad! Thought it might be related. WAMP can be awesome... or a total pain in the ass. Good luck! – Chris Baker Feb 12 '13 at 21:22