Jump to content

[SOLVED] ajax question, making mulitple XMLHTTPRequest's at the same time


s0c0

Recommended Posts

When I make more than one XMLHTTPRequest inside a javascript function I get errors and exceptions.  So I have to encapsulate other XMLHTTPRequest functions inside the setTimeout function, to pause the functions time of execution.  Is this a valid way of handling such a problem?  Is this really just some lowly hack?  Is there a better way to do what I am attempting?

 

Please advise.

Link to comment
Share on other sites

I'd call it a lowly hack in that it will not prevent you from running into problems.

 

Also, you must remember that most browsers will only hold two connections to a server open at a time.  You have two options here.

 

1)  Implement a Javascript priority queue.  Instead of enacting the request right away, place it on the queue to be executed later when a request is available.  You can only ever have two requests happening at once, so bear that in mind before allowing the queue to launch another request.  Also, you should implement age-based priority to prevent a low-priority request from sitting in the queue forever.

 

2)  Reduce the number of AJAX requests.  I had a situation in my own Javascript program where it would send a request to the server, wait for the response, and based on the response send another request to the server.  The second request was dependent on the first request, so I used a synchronous request in the first request, which would lock the UI until it returned.  Eventually I realized I only needed one request.

 

Example of two requests (pseudo code)

  request1 = send_sync_ajax_request()
  if(request1 == ONE_VALUE){
    request2 = send_async_ajax_request()
  }else{
    request2 = send_other_async_ajax_request()
  }
  alert( request2 );

 

Here we have two AJAX requests when we really only need one.  Instead of performing the logic of testing the result of request1 on the client, why not just test the result on the server and invoke the second request from the server?

 

Reducing requests down to 1 (pseudo code)

  // Javascript
  request = send_ajax_request();
  alert( request );

  // PHP Script on the server
  $result1 = perform some action based on $_POST / $_GET parameters
  if($result1 == ONE_VALUE){
    $result2 = perform one thing
  }else{
    $result2 = perform another thing
  }
  $JSON = Array( "result1" => $result1, "result2" => $result2 );
  echo Array2JSON($JSON);

 

A real world example would be the user enters a coupon code at a shop; if the coupon is valid you return a list of items it applies to, if the coupon is invalid you return a list of items that are still bargains and similar to the items the coupon would have applied to.

 

You must validate the coupon on the server so the client can't bypass it.  So with two requests you'd do the validation, then send another request based on the validation.  But really you could send the coupon code, determine if it's valid, and return the proper list from the server right from the beginning, thus using a single request.

Link to comment
Share on other sites

I think you're correct roopurt.  I should have one xmlhttprequest trigger multiple method calls server-side and just return more data at once to prevent multiple requests.  Damnit, I'm sick of fixing things, this is what happens when management sets strict unobtainable deadlines that I told them could not be met in the first place and they do not allow for proper testing of the application...ARRRRGGGGHHHHHHH!!!!!!!  Working at home tonight...ROUND 10...FIGHT!

Link to comment
Share on other sites

One request dependent on another: you can do that without using a sync request. You can use setTimeOut to continue to check if the first response has been received, until it has, then you stop the timeout function and do the second request. The UI never freezes and the dependency is maintained.

Link to comment
Share on other sites

Naturally if you can avoid multiple requests, you should definitely do so. In any case, I recommend to try to avoid synchronous requests at all times.

 

Just for correctness: my previous post said SetTimeOut, I meant SetInterval. Pardon...  :-[

Link to comment
Share on other sites

I agree about avoiding synchronous requests, however I have used them in minor places when they're expected to be quick.  One place where I'll usually use a synchronous request is on saving data.  I don't want the client to click off the page during a save so that they miss the status message for the request.  I could create an iframe and post the results into there so that even if they leave the page they're notified of the save status; but I doubt my boss would want me to take the time to implement that and then go back and upgrade the few areas that need it.  Bleh!

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.