1) Script Title: Basic Ajax Routine (get & post)

2) Script URL (on DD): http://www.dynamicdrive.com/dynamici...jaxroutine.htm

3) Describe problem:
Nice forum...
I have written a Chat based on the Basic Ajax Routine and combined it with JSON (JavaServerPages for generating the JSON-code). It looks like this:

Part 1: Javascript
<script language="javascript" src="js/ajaxroutine.js"></script>

Part 2: Serverside Document:
the output looks like this (http://www.dynamicajax.com/fr/JSON_A...290_324.html):
Code:
{"messages": 
	{"message":[ 
		{"id":  "17",
			"user": "Ryan Smith",
			"text": "This is an example of JSON",
			"time": "04:41"
		},{"id":  "18",
			"user": "Ryan Smith",
			"text": "Here is another Element",
			"time": "04:41"
		} ]
	}
}
Part 3: Display the results in DIV.

Code:
<script type="text/javascript">

	var lastMessageDateTime = '2007-01-01 00:00:01';

//Step 2: Define a "callback" function to process the data returned by the Ajax request:
function processGetPost()
{
	var myajax=ajaxpack.ajaxobj
	var myfiletype=ajaxpack.filetype
	var mydisplay=ajaxpack.g_layer //(I defined this one in ajaxroutine.js)
	
	if (myajax.readyState == 4)
	{ //if request of file completed
	
		var displayScroll = top.document.getElementById(mydisplay);
		
		if (myajax.status==200 || window.location.href.indexOf("http")==-1)
		{ //if request was successful or running script locally
			if (myfiletype=="txt")
			{
				var place = top.document.getElementById(mydisplay);
				var pastData = place.innerHTML;
				
				var responseJSON = eval("(" + myajax.responseText + ")");
				
				
				
				for(i = 0; i < responseJSON.msgparent.msg.length; i++)
				{
				
					place.innerHTML += '<div id=\'userMessageHeader\'>' +responseJSON.msgparent.msg[i].itemusername 
					+' [' +responseJSON.msgparent.msg[i].itemtimef +']</div>';
					place.innerHTML += '<div id=\'userMessage\'>' +responseJSON.msgparent.msg[i].itemmessage + '</div>';
	
					lastMessageDateTime = responseJSON.msgparent.msg[i].lastmsgdatetimeall;
					
					displayScroll.scrollTop = displayScroll.scrollHeight;
				} // end for responseJSON
                                  refresh here ???
		
			} // end myfiletype equals txt
			else
			{
				//alert("TypXML:" +myajax.responseXML)
			}
		} // end status == 200
	} // end readystate == 4
	
}


function initChat()
{
	top.document.getElementById('live_msg').focus();
	top.ajaxpack.getAjaxRequest('functions/getFullChat.jsp', lastMessageDateTime, processGetPost, 'txt', 'msgDisplay');

}

function refreshChat(lastMsgDT)
{
    // here is what i tried to do:
    top.ajaxpack.getAjaxRequest('functions/getLastChatMsg.jsp', live_msgPandV, processGetPost, 'txt', whichDisplay);
}


function submitLiveMsg(whichInput, whichDisplay)
{
	var live_msg = top.document.getElementById("live_msg").value 
	
	// no message entered
	if(live_msg  == '')
	{
		alert("Please enter a message.");
		top.document.getElementById(whichInput).style.backgroundColor='#FFCCCC';
		
		top.document.getElementById(whichInput).focus();	

		return false;
	}
	else
	{
		//clearInterval(refreshTimer);
		var live_msgPandV = whichInput +"=" + encodeURI(live_msg) +'&lastMessageDateTime=' +lastMessageDateTime  // Param and Value
		
		top.ajaxpack.getAjaxRequest('functions/getLastChatMsg.jsp', live_msgPandV, processGetPost, 'txt', whichDisplay);
		top.document.getElementById(whichInput).value = '';
	}
}

</script>
finally I made this:
<body onload="initChat()">

so far so good. It all works fine! each time i enter a keyword into the inputfield it's
sent to the server,
db is updated,
newest entries are taken again from db,
formatted into JSON,
returned to clientside,
cut by the eval() and for-loop,
and my div-tags are written again.
BUT. now i tried to create some automated refreshs (each 2 secs.), but for some reason it didn't work: sometimes the IE freezs and sometimes there is no refresh.
i would be glad if someone can tell me where and how to insert the timer, and if the line getAjaxRequest('functions/getLastChatMsg.jsp', live_msgPandV, processGetPost, 'txt', whichDisplay); placed after the loop is for the refresh (each 2 secs) correct.


thanks a lot!!!!


lgman