By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,107 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,107 IT Pros & Developers. It's quick & easy.

Ajax Call Tracking failed attempt

P: 17
I attempted to synchronize async Ajax calls using the following JS blocks:
================================================== ==
Expand|Select|Wrap|Line Numbers
  1. function getXMLHTTPRequest()
  2. {
  3.   try
  4.   {
  5.     req = window.XMLHttpRequest ? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
  6.   }
  7.   catch (err) { }
  8.   return req;
  9. }
  10.  
  11. // update the window status 
  12. function updateStatus()
  13. {
  14.   window.status += '-';
  15. }
  16.  
  17. var httpReq = null;
  18. function loadURL(dest, trigfunc)
  19. {
  20.   // create only when needed
  21.   if (httpReq == null) { alert("Create httpReq"); httpReq = getXMLHTTPRequest(); }
  22.  
  23.   while (1)
  24.   {
  25.     // only sumbit another Ajax calls when the readsyState is 0
  26.     if (httpReq.readyState == 0)
  27.     {
  28.       httpReq.onreadystatechange = trigfunc;
  29.       httpReq.open("GET", dest, true);
  30.       httpReq.send(null);
  31.       break;
  32.     }
  33.     // delay the while loop execution for 500 milisec by updating the status line
  34.     else window.setTimeout("updateStatus()", 500);
  35.   }
  36. }
  37.  
  38. // Ajax triggered function processes different business logic depending on the
  39. // content of responseText
  40. function trigFunc()
  41. {
  42.   if (httpReq.readyState == 4)
  43.   {
  44.     // Process the responseText from httpReq
  45.     ..................
  46.  
  47.     // to reset the httpReq.readyState to 0 so next Ajax calls can be submitted
  48.     httpReq.abort(); 
  49.   }
  50. }
  51.  
  52. function myFunc()
  53. {
  54.   // Test for some condition on the web page
  55.   if (condition is true)
  56.     loadURL(urlOne, trigFunc);
  57.  
  58.   loadURL(urlTwo, trigFunc);
  59. }
  60.  
=============================================

I thought after the loadURL(urlOne, trigFunc) call made, loadURL(urlTwo, trigFunc) call will be made but never submitted until the triggered function from the first call executed and reset the readyState to 0.

While the first call is executed on the server side, window will stay in the loop and update the status.

IT NEVER WORKED. For some reason, the result of second call is lost and window stuck in the forever loop.

Can anyone offer some hint to my problem ? Thanks
Jul 25 '07 #1
Share this Question
Share on Google+
3 Replies


epots9
Expert 100+
P: 1,351
since u are trying to run 2 requests at the same time, it won't work. cuz while one is going your overwritting it with another, so cancelling the first one.

your httpReq object holds everything to make the ajax work, so while its running your changing its properties.

instead, create 2 httpReq objects that way they will both run correctly.
Jul 25 '07 #2

P: 17
But I tried to synch the Ajax request with the while loop
while (1)
{
if (httpReq.readystate == 0)
{ ... }
else { .... setTimeout}
}

what is wrong with this code ? Thanks
Jul 25 '07 #3

Plater
Expert 5K+
P: 7,872
The readystate will never return to zero if an open call has been made.
When the state=4, it is done, grab the content and then make another call to .open() and send() and the like.
Jul 25 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.