Jump to content

AJAX takes an age and freezes up my app


eoino

Recommended Posts

I know its something in me code but as I'm new to AJAX and have little to NO support at hand here, I was hoping someone could point out the probably glaring reason for the page stalling on me.

 

Basic form that allows a user to input values and get calculated results on the fly as those values are entered into the fields. The page uses AJAX to pass the values to two different pages (one jsp and one php) to calculate different formulas and then display the results to screen.

 

I use the one js page to access and pass data between the original page and the two calculator pages.

 

var xmlHttp
var xmlHttp2

// Main loan details function
function showResults(str, str2, str3, str4, str5, str6, str7, str8, str9, str10, str11 )
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Your browser does not support the XMLHttpRequest object.")
return
}

// Construct the url
var url="myJSPcalculationpage.jsp"
url=url+"?calc1="+str
url=url+"&calc2="+str2
url=url+"&calc3="+str3

url=url+"&calc4="+str4
url=url+"&calc5="+str5
url=url+"&calc6="+str6
url=url+"&calc7="+str7
url=url+"&calc8="+str8

url=url+"&calc9="+str9
url=url+"&calc10="+str10
url=url+"&calc11="+str11


url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)

showResults2 (str, str2, str3, str4, str5, str6, str7, str8 )

}

function stateChanged()
{
  if (xmlHttp.readyState == 0)
  {
document.getElementById("txtResults").innerHTML = "<p> Calculating ...</p>"; //loading
  }
  else if(xmlHttp.readyState == 1)
  {
document.getElementById("txtResults").innerHTML = "<p> Calculating...</p>"; //loaded
  }
  else if(xmlHttp.readyState == 2)
  {
document.getElementById("txtResults").innerHTML = "<p> Calculating...</p>"; //interactive
  }
  else if(xmlHttp.readyState == 3)
  {
document.getElementById("txtResults").innerHTML = "<p> Loading data...</p>";
  }
  else if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  {
  document.getElementById("txtResults").innerHTML=xmlHttp.responseText  
  }

}

// XML object for first function
function GetXmlHttpObject()
{
  var objXMLHttp=null
  
  try {
    objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP"); //later IE
  } catch (e) {
  try {
    objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP"); //earlier IE
  } catch (e) {
  objXMLHttp = null;
  }
  }
  
  if (objXMLHttp==null)
  {
    objXMLHttp=new XMLHttpRequest() //IE7, Firefox, Safari
  }
  return objXMLHttp
}


// Call second function
function showResults2(istr, istr2, istr3, istr4, istr5, istr6, istr7, istr8)
{
xmlHttp2=GetXmlHttpObject()
if (xmlHttp2==null)
{
alert ("Your browser does not support the XMLHttpRequest object.")
return
}

var iurl="myPHPcalculatorpage.php"
iurl=iurl+"?calci1="+istr
iurl=iurl+"&calci2="+istr2
iurl=iurl+"&calci3="+istr3
iurl=iurl+"&calci4="+istr4
iurl=iurl+"&calci5="+istr5
iurl=iurl+"&calci6="+istr6
iurl=iurl+"&calci7="+istr7
iurl=iurl+"&calci8="+istr8

iurl=iurl+"&sid="+Math.random()
xmlHttp2.onreadystatechange=stateChanged2
xmlHttp2.open("GET",iurl,true)
xmlHttp2.send(null)
}


function stateChanged2()
{
  if (xmlHttp2.readyState == 0)
  {
document.getElementById("txtResults2").innerHTML = "<p> Calculating...</p>"; //loading
  }
  else if(xmlHttp2.readyState == 1)
  {
document.getElementById("txtResults2").innerHTML = "<p> Calculating...</p>"; //loaded
  }
  else if(xmlHttp2.readyState == 2)
  {
document.getElementById("txtResults2").innerHTML = "<p> Calculating...</p>"; //interactive
  }
  else if(xmlHttp2.readyState == 3)
  {
document.getElementById("txtResults2").innerHTML = "<p> Loading data...</p>";
  }
  else if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete")
  {
  document.getElementById("txtResults2").innerHTML=xmlHttp2.responseText
  }

}

// XML object for second function
function GetXmlHttpObject2()
{
  var objXMLHttp2=null
  
  try {
    objXMLHttp2 = new ActiveXObject("Msxml2.XMLHTTP"); //later IE
  } catch (e) {
  try {
    objXMLHttp2 = new ActiveXObject("Microsoft.XMLHTTP"); //earlier IE
  } catch (e) {
  objXMLHttp2 = null;
  }
  }
  
  if (objXMLHttp2==null)
  {
    objXMLHttp2=new XMLHttpRequest() //IE7, Firefox, Safari
  }
  return objXMLHttp2
}

 

I output the results to two different panes on my original page. It works fine if all the strings being passed to both pages have values but it freezes up the entire page in readystate 2 if all values are not present.

 

Im guessing ive broken so may rules here its not even funny (or maybe it is), but if anyone could help me out that would be great.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.