473,395 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Ajax Call Tracking failed attempt

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
3 2711
epots9
1,351 Expert 1GB
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
nghivo
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
7,872 Expert 4TB
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

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

Similar topics

31
by: Tony | last post by:
I just noticed that prototype.js is one of the files in the Ajax.NET distribution - I'm pretty concerned about this. Does anyone know if this is the same "prototype.js" that is not well-liked...
3
by: equazcion | last post by:
Hi, I have an image reference (IMG) in my page that changes depending on the value of a database field. Clicking the image triggers an Ajax call to change the database field (toggles the field...
17
by: Arjen | last post by:
Hi, I want to reload 2 divs at one click. Ive tried: <a href = "javascript:void(0);"...
1
by: bizt | last post by:
Hi, I am having my first real attempt at an ajax class as so far Ive managed to build one that, once instatiated, will allow me to define which function it will call on completion then retrieves...
1
by: Ryan Liu | last post by:
Hi, I have a 100 clients/ one server application, use ugly one thread pre client approach. And both side user sync I/O. I frequently see the error on server side(client side code is same, but...
5
by: KDawg44 | last post by:
Hello, I am beginning to work on a project for my degree. The concept is "The Utilization of AJAX calls to separate the Presentation Layer and Application Layer in Web Applications." Now, I...
9
bugboy
by: bugboy | last post by:
I have a page being generated with PHP, it includes a DIV who's content gets swapped with ajax. the new content contains the following function call PHP echo'd inside the parent DIV. The code...
7
by: Andrew Poulos | last post by:
I'm working on a "simple" tracking system for a client that will be receiving elearning packages from 3rd parties. I'm intending to do the communication with the database using ajax. My...
1
by: javediq143 | last post by:
Hi All, This is my first post in this forum. I'm developing a CMS for my latest website. This CMS is also in PhP & MySQL. I'm done with the ADD section where the Admin can INSERT new records in...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.