I found this code, which is like a Long Polling , and wanted to know if I might have problems using it. And I also wanted to know the cons of this code if I use it.
pagina.html
= > page that the automatic update occurs
<!DOCTYPE HTML>
<html>
<head>
<meta charset="iso-8859-1">
<title>Untitled Document</title>
<script src="ajax.js" language="JavaScript" type="text/javascript"></script>
<script type="text/javascript">
obj_online = new montaXMLHTTP();
function Online(){
obj_online.open("GET","ultimasmensagens.php",true); // Na pagina ultimassenhas esta a programação que lista as informações do BD
obj_online.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
obj_online.onreadystatechange = function(){
if(obj_online.readyState == 4){
document.getElementById("online").innerHTML = obj_online.responseText;
clearTimeout(re);
setTimeout("Online()",5000);
}
}
obj_online.send(null);
var re = setTimeout("reenvia()",10000);
}
</script>
</head>
<body onLoad="setTimeout('Online()',2000);">
<div id="online">
</div>
</body>
</html>
ajax.js
function montaXMLHTTP(){
try{
myObj = new XMLHttpRequest()
}catch(e){
myObj = new ActiveXObject("Microsoft.XMLHTTP");
}
return myObj;
}
ultimasmensagens.php
= > page that lists the messages
<!DOCTYPE HTML>
<html>
<head>
<meta charset="iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<?
include 'config.php'; // conexao com o bd
$mensagens = mysql_query("SELECT * FROM mensagens order by idmensagem DESC limit 5")
or die (mysql_error());
while($minhalista = mysql_fetch_array($mensagens)){ ?>
<?=$minhalista['titulo']?><br />
<? } ?>
</body>
</html>