Tyson
Legacy Member
Werking: Client checkt elke 1.5 seconden of er nieuwe tekst is om weer te geven.
Probleem: CPU load van de client stijgt naarmate de pagina langer open staat. Zelfs als er geen nieuwe gegevens zijn.
Code (ingekorte versie):
Alvast bedankt voor het kijken.
Probleem: CPU load van de client stijgt naarmate de pagina langer open staat. Zelfs als er geen nieuwe gegevens zijn.
Code (ingekorte versie):
Code:
<html>
<head>
<script language="javascript" type="text/javascript">
var xmlHttpText = getXmlHttpObject();
var textTimer;
var lastEntry = <?php include_once("chatDao.php"); $chatDao = new ChatDao(); echo $chatDao->getChatId(); ?>;
function getText()
{
if(document.getElementById("autofocus").checked == true)
{
document.getElementById("text").focus();
}
url = "getText.php?lastEntry=" + lastEntry;
xmlHttpText.onreadystatechange = responseText;
xmlHttpText.open("GET", url, true);
xmlHttpText.send(null);
}
function getXmlHttpObject()
{
var xmlHttp;
try
{
xmlHttp = new XMLHttpRequest();
}
catch(trymicrosoft)
{
try
{
xmlHttp = new ActiveXObject("Msxml12.XMLHTTP");
}
catch(failed)
{
try{
xmlHttp = new window.createRequest();
}catch (e){
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
xmlHttp = null;
}
}
}
}
return xmlHttp;
}
function responseText()
{
if(xmlHttpText.readyState == 4)
{
if(xmlHttpText.status == 200)
{
if(document.getElementById("autorefresh").checked == true)
{
lastEntryTemp = xmlHttpText.responseText.substr(xmlHttpText.responseText.lastIndexOf(";") + 1);
if(lastEntryTemp > lastEntry)
{
lastEntry = lastEntryTemp;
}
if(xmlHttpText.responseText.substr(0, xmlHttpText.responseText.lastIndexOf(";")).length > 0)
{
document.getElementById("chat").innerHTML += xmlHttpText.responseText.substr(0, xmlHttpText.responseText.lastIndexOf(";"));
document.getElementById("chat").scrollTop = document.getElementById("chat").scrollHeight;
}
}
}
clearTimeout ( textTimer );
textTimer = setTimeout('getText();',1500);
}
}
</script>
</head>
<body onLoad="getText();">
<div class="chat" id="chat" style="height: 400px; overflow: auto;">
<input type="checkbox" name="autofocus" id="autofocus" checked> Autofocus?
<input type="checkbox" name="autorefresh" id="autorefresh" checked> Autorefresh?
</body>
</html>
Alvast bedankt voor het kijken.
