I made a code so that when I changed something in a div, it automatically changed the page in real time without having to give refresh or anything, however when I change the contents of the div if by chance f5, the information appears repeatedly.
function Ajax(){
var xmlHttp;
try{
xmlHttp=new XMLHttpRequest();// Firefox, Opera 8.0+, Safari
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("No AJAX!?");
return false;
}
}
}
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
document.getElementById('ReloadThis').innerHTML=xmlHttp.responseText;
setTimeout('Ajax()',10);
}
}
xmlHttp.open("GET","index.php",true); // aqui configuramos o arquivo
xmlHttp.send(null);
}
window.onload=function(){
setTimeout('Ajax()',10); // aqui o tempo entre uma atualização e outra
}
<div style="background-color:#00CED1;" id="ReloadThis"></div>
teste
When executing the code if f5 is given on the page, the message "test" appears 2x.
I wondered if there was any way to fix this.
Thank you