[jQuery] waiting for a request to complete

2007-06-11 Thread Phil Glatz

I'm calling $.post to send some text back to a web server, where it
gets logged. The function is something like this:

function send_message(msg) {
  var params = {};
  params['my_message'] = msg;
  $.post(/myhandler.php, params);
}

It calls a php page that appends it to a log. If I view the log in
real time with tail -f log.txt, I can see the messages.

The problem is that if I set up a page to send 5 messages, they aren't
being displayed sequentially; I might see them in the order 2,1,4,3,5
-- and the order is random each time I call the page. What I would
like to do is wait until the post function completes before continuing
with the next message.

Since the post returns an XMLHttpRequest object, I thought I could try
some thing like:

  while ($.post(/myhandler.php, params).status != 200)

But I think that simply keeps calling the function, making things
worse.

How can I query the post request for success, and not exit my function
until it is done?



[jQuery] waiting for a request to complete

2007-06-11 Thread Phil Glatz

Rob suggested:
 Alternatively you could use a globally accessibly variable which states
 whether there is a pending request or not.
 When a request is sent, set it true, when the response is received set
 it false.

Something like this?


var pending = false;

function send_message(msg) {
  pending = true;
  var params = {};
  params['plugger_debug_message'] = msg;
  $.post(/myhandler.php, params, function() { pending = false;});
  while (pending);
}


I had also tried that, but I get a browser timeout after 30 seconds.
Any more thoughts?

thanks



[jQuery] waiting for a request to complete

2007-06-11 Thread Phil Glatz

Rob wrote:
Where you're checking the request status you're creating a new request
each time.
You need to store the XHR object from the request and use that
(untested):

var xhr = $.post(/myhandler.php, params);
while (xhr.status != 200);


With Firefox 2, I'm getting this error:

[Exception... Component returned failure code: 0x80040111
(NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status] nsresult:
0x80040111 (NS_ERROR_NOT_AVAILABLE) location: JS frame ::
http://pglatz.lifewire.com/sites/pglatz.lifewire.com/modules/plugger/plugger_category.js
:: plugger_debug :: line 194 data: no]
[Break on this error] while (xhr.status != 200);

This is part of a Drupal site, if that makes any difference.

thanks