zod
zod

Reputation: 12437

How to find javascript Ajax call is completed

function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
}



 if (xmlhttp.readyState==4 && xmlhttp.status==200)

Do the above statement means that the process is complete.

I am calling a db procedure in the file and it may take a little time.

do this status 4 means that the procedure run is complete?

how can i call another function if this procedurew call is complete

Upvotes: 2

Views: 990

Answers (1)

Jonathan M
Jonathan M

Reputation: 17461

The function assigned to xmlhttp.onreadystatechange executes every time the http state changes. When the state is 4 and the status is 200, the request has completed successfully. Your code is correctly checking for that. You can call another function from within the body of the if conditional that checks for the state and status.

If you're not just trying to learn the ins and outs of ajax, and just want to get on with it, I suggest you use the jQuery library. There are many things to check for and catch in ajax. It's more than meets the eye. So, if you're just wanting to move on to the job of getting your program going, take a good look at jQuery. It handles all of the tricks and traps for you, including all the differences between browsers.

Upvotes: 2

Related Questions