Solved

Ajax : Multiple calls..

Posted on 2006-11-05
9
194 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Find out all repos that a user is most active on Github 1 30
Triple line in an alert message 4 24
arrays and buttons with user input 2 34
HTML editor custom button 3 59
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

733 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