I have the code to do an ajax call isolated in a function. When I call it multiple times, in some instances, it will return what ever was in the call of the last call.
Please show me how to code this so this does not happen while keeping it a called function.
Here is the code:
function doajax(arg,descs){
var DATETIME = new Date().getTime();
arg = arg+'&DATETIME='+DATETIME
if (window.XMLHttpRequest) objHTTP = new XMLHttpRequest();
else if (window.ActiveXObject) {
try {
objHTTP = new ActiveXObject("Msxml2.XMLH
TTP")
} catch(e) {
try {
objHTTP = new ActiveXObject("Microsoft.X
MLHTTP")
} catch(e) {}
}
}
else return;
objHTTP.onreadystatechange
= function() {
if(objHTTP.readyState == 4){
var tvar = objHTTP.responseText.subst
ring(objHT
TP.respons
eText.inde
xOf('<HTML
>')+6,objH
TTP.respon
seText.ind
exOf('</HT
ML>'));
var tarr = tvar.split('(*~$)');
var descsarray = descs.split(',');
for (i=0;i<descsarray.length;i
++){docume
nt.getElem
entById(de
scsarray[i
]).innerHT
ML = tarr[i];}
}
};
objHTTP.open("GET", arg, true);
objHTTP.send('');
return;
}
If I call it like this, it works fine:
function callmultiple{
arg = '
http://whatever.com/cgi/prog.pl?program=test&div=div1&div=div2';
descs = 'div1,div2';
doajax(arg,descs);
return
}
The above was my workaround, here is my original code:
function callmultiple{
arg = '
http://whatever.com/cgi/prog.pl?program=test&div=div1';
descs = 'div1';
doajax(arg,descs);
arg = '
http://whatever.com/cgi/prog.pl?program=test&div=div2';
descs = 'div2';
doajax(arg,descs);
return
}
Please do not check the syntax of the calling functions as they are just samples.
I am sure there is a timing issue with calling the doajax twice in a row as it does not wait to call it a second time. I do not know how to avaoid this.
Thanks in advance for your help, Troy