Double Message Error Javascript Ajax

Asked

Viewed 46 times

2

I tried to make a code so that when I changed something in a div, I would automatically change the page in real time without having to refresh or anything like that, however when I change the content of the div if it happens to be F5, the information appears repeated.

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 you give F5 on the page appears 2x the message "test".

I was wondering if there was any way to fix this.

Thank you

1 answer

2

Look, the text teste is out of the Div#Reloadthis, so it is not being replaced. then just change it into the DIV.

//evitar o uso de try-cacth dentro do setTimeout.
var HttpRequest = window.XMLHttpRequest;
var requestConfig = null;

if (!HttpRequest) {
  HttpRequest = ActiveXObject;
  try {
    new ActiveXObject("MSXML2.XMLHTTP.6.0");
    requestConfig = "MSXML2.XMLHTTP.6.0";
  } catch (e) { 
    try {           
      return new ActiveXObject('MSXML2.XMLHTTP.3.0');
      requestConfig = "MSXML2.XMLHTTP.3.0";
    } catch (e) { 
      HttpRequest = null;
      return null;
    } 
  }
}

var ajax = function (url, target){
  var xmlHttp = new HttpRequest(requestConfig);     
  xmlHttp.onreadystatechange=function(){
    if(xmlHttp.readyState == 4 && xmlHttp.status == 200){
      target.innerHTML=xmlHttp.responseText;
      interval(url, target);
    }
  }
  xmlHttp.open("GET", url, true); // aqui configuramos o arquivo
  xmlHttp.send();
}

var interval = function (url, target) {
  setTimeout(function () {
    ajax(url, target)
  }, 10); // aqui o tempo entre uma atualização e outra
}

if (HttpRequest) {
  window.onload=function(){
    var target = document.getElementById('ReloadThis');
    interval("index.php", target);    
  }
}
<div style="background-color:#00CED1;" id="ReloadThis">
  teste
</div>

I also fixed an issue regarding your AJAX request, it does not check if it ended successfully, so I added a status == 200

In any case, I believe that your solution has other problems, maybe you should rethink what content needs to be updated, when, and even if you should use sockets or just search for new content. because making a request every 10ms is at least a bad idea.

  • Tell me something, because even though I put things outside the div, the site continues to update things that are not inside the div in real time?

  • your AJAX basically overrides the content of div#ReloadThis, I believe that your index.php have only the text teste, then at the end of the AJAX request, it will update the content div#ReloadThis with the text teste and keep the text teste out of the intact div... if you write in the arquivo.php hello world, then on your page will appear hello world teste.

  • How do I only update the div in real time without adding anything else?

  • Gonçalo, your div is already being updated in real time, the only reason to appear the text teste 2x, is pq the same is initially outside the div to be updated.

  • How do I correct that?

  • When I put something outside the div, this thing continues to be updated in real time, even if not in the div and when I give F5 appears 2x.

  • Gonçalo, I believe you have two html/php files, the first one where this script is together with the div and the second with the page fragment with the content to be updated in real time... your script updates only the content of div#ReloadThis on its first page with the contents of Fragment (second page), if it is not behaving this way, check that all its html tags are closing in the correct way.

  • You can simplify my text, I’m a little new in the field and I don’t understand some terms.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.