Jump to content

Ajax timeout issue


prime

Recommended Posts

I have the following code for a chat display app I made and well it works perfectly on some browser but not others, can someone tell me where I went wrong with it please...

 

I think it's something to do with the recursion, but I'm not 100% sure

 

 

<html><head>



<script src="../ajaxrequest.js"></script>
<script langauge="JavaScript">function updatePage()
{ if(request.readyState == 4) {



var xmlobj = request.responseXML
var id = xmlobj.getElementsByTagName("postid")[0].childNodes[0].nodeValue
var user = xmlobj.getElementsByTagName("user")[0].childNodes[0].nodeValue
var message = xmlobj.getElementsByTagName("message")[0].childNodes[0].nodeValue

usermessage = user + ": " + message;

if(previd == undefined)
{
Display = "<p>Welcome to Primefalcon's AJAX chat have a seat and enjoy your stay</p><br /><br />";
}
else if(previd == id)
{
Display = prevdata;
}
else
{
Display = prevdata + "<br />" + usermessage;
}
document.getElementById("pagedisplay").innerHTML = Display;



previd = id;
prevdata = Display;
}}</script>
<style type="text/css">
body
{
background-color: #000066;
}
#pagedisplay
{
color: #000000;
}
</style>
</head><body bgcolor="white"><font color="black">
<script langauge="JavaScript">
var previd;
var prevdata;
function main()
{
	createRequest();
	var serverurl = "server_display.php";
	request.open("GET", serverurl, true);
	request.onreadystatechange = updatePage;
	request.send(null); //just add + for extra info
	setTimeout('main()', '300');
}

main();
</script>

<div id="pagedisplay"></div>

</body></html>

Link to comment
Share on other sites

My guess would be that the main function is getting executed more than once at a time.

 

 

Couple of ways to fix this problem:

 

-Make the setTimeout('main()', '300'); call in the callback function (updatePage).  That way, it will be set to run again right after it finishes.

-Don't rely on global variables.  For example, instead of using request in updatePage (since request is a global variable), use this.  this can be used since the method is called form inside of the function.  Then, it doesn't matter what the outside variable is.  Also, make the createRequest() function return a value, not set a global var.

Link to comment
Share on other sites

1 question, the request as in request is my Ajax object which is...

 

var request = null;


function createRequest()
{
try
{ request = new XMLHttpRequest(); }

catch(trymicrosoft)
{ try { request = new ActiveXObject("Msxml2.XMLHTTP"); }

catch(othermicrosoft)
{ try { request = new ActiveXObject("Microsoft.XMLHTTP"); }

catch(failed)
{ request = null; } } }  }

Link to comment
Share on other sites

I would be more likely to do it like this:

 

function createRequest() {
var request;
try {
	request = new XMLHttpRequest();
}
catch(trymicrosoft) {
	try {
		request = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(othermicrosoft){
		try {
			request = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(failed) {
			request = false;
		}
	}
}
}

var request = createRequest();
if(request === false) {
alert("Please get a browser with AJAX support.");
}

 

 

If it returns a new instance of an AJAX object, it can be used more easily, and it doesn't rely on a global variable (global variables are evil ;p).

 

If you want, I can tell you the changes I would make to all of your code.

Link to comment
Share on other sites

<html>
<head>
<script src="../ajaxrequest.js"></script>
<script language="JavaScript">
function createRequest() {
var request;
try {
	request = new XMLHttpRequest();
}
catch(trymicrosoft) {
	try {
		request = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(othermicrosoft){
		try {
			request = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(failed) {
			request = false;
		}
	}
}
return request;
}

function updatePage() {
if(this.readyState == 4) {
	var xmlobj = this.responseXML;
	var id = xmlobj.getElementsByTagName("postid")[0].childNodes[0].nodeValue;
	var user = xmlobj.getElementsByTagName("user")[0].childNodes[0].nodeValue;
	var message = xmlobj.getElementsByTagName("message")[0].childNodes[0].nodeValue;

	usermessage = user + ": " + message;

	if(previd == undefined {
		Display = "<p>Welcome to Primefalcon's AJAX chat have a seat and enjoy your stay</p><br /><br />";
	}
	else if(previd == id) {
		Display = prevdata;
	}
	else
	{
		Display = prevdata + "<br />" + usermessage;
	}
	document.getElementById("pagedisplay").innerHTML = Display;	
	previd = id;
	prevdata = Display;
	setTimeout('main()', '300');
}
}

var previd;
var prevdata;
function main() {
var request = createRequest();
var serverurl = "server_display.php";
request.open("GET", serverurl, true);
request.onreadystatechange = updatePage;
request.send(null); //just add + for extra info
}

</script>
<style type="text/css">
body {
background-color: #000066;
color: black;
}
#pagedisplay {
color: #000000;
}
</style>
</head>
<body bgcolor="white">
<script language="JavaScript">
	main();
</script>

<div id="pagedisplay"></div>

</body>
</html>

 

 

Oh, you misspelled language every time it was in there by the way x.x.

 

 

Anyway that's more-ish how I would do it.

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.