Solved

Ajax : Multiple calls..

Posted on 2006-11-05
9
193 Views
Last Modified: 2008-01-09
Hi all,
I have a page with some simple Ajax code on it, its called a number of time to populate elements on the page. The probem is that if one ajax call is running when another starts up, it cancels the first out... anyone know of a way around this?

Here is the code im using: (If I stick an alert() at the start to force a delay it works ok, but thats not really an idea solution).

function sndReq() {
   theTarget = htmltarget
myRand=parseInt(Math.random()*99999999);
   http.open('get', 'http://www.domain.com/');
   http.onreadystatechange = handleResponse;
   http.send(null);
}

function handleResponse() {
 if(http.readyState == 4 && http.status == 200){
       var response = http.responseText;

           document.getElementById(theTarget).innerHTML = response;

   }
}
0
Comment
Question by:fox_statton
9 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 17877638
The problem is that you use only ONE http object for multyple http requests.
Create multiple http Objects with different names and use each for its particular request and response.

0
 

Author Comment

by:fox_statton
ID: 17877847
How, how do I create different named http objects?
0
 
LVL 17

Expert Comment

by:gops1
ID: 17878550
Otherwise, store in a global variable, the number of times the call to be made. Later you can make multiple calls here

var noOfCalls=get The Number Of Calls;
var calStatus=0;

function handleResponse() {
 if(http.readyState == 4 && http.status == 200){
       var response = http.responseText;

           document.getElementById(theTarget).innerHTML = response;
           calStatus++;
           if(calStatus<noOfCalls){ sndReq(); }

   }
}
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:fox_statton
ID: 17880205
Hi Zvonko,

can you tell me how the multiple named http objects would be done?

Thanks
0
 
LVL 12

Accepted Solution

by:
netsmithcentral earned 498 total points
ID: 17883059
Make the http_request a local variable.  That will mean every time you make a new request, a new instance of that local variable appears.

function sndReq() {
   var http;
   if (window.XMLHttpRequest){
      http = new XMLHttpRequest();
   } else if (window.ActiveXObject) {
      try {
         http = new ActiveXObject('Msxml2.XMLHTTP');
      } catch (e) {      
         try {
            http = new ActiveXObject('Microsoft.XMLHTTP');
         } catch (e) {}
      }
   }
   theTarget = htmltarget
   myRand=parseInt(Math.random()*99999999);
   http.open('get', 'http://www.domain.com/');
   http.onreadystatechange = function(){handleResponse(http);};
   http.send(null);
}

function handleResponse(httpObject) {
 if(httpObject.readyState == 4 && httpObject.status == 200){
       var response = httpObject.responseText;

           document.getElementById(theTarget).innerHTML = response;

   }
}
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 17884806
Show me your actual code and I can show you how to easy extend the code to be multy Object featured.

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how can display objects ? 3 33
JavaScript/CSS: Detect if hue-rotate supported by browser 1 34
How do specify keydowns in JavaScript 3 44
Jquery 10 19
Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now