Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Ajax : Multiple calls..

Posted on 2006-11-05
9
Medium Priority
?
199 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the 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 1992 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

705 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