Solved

Ajax : Multiple calls..

Posted on 2006-11-05
9
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

734 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