Ajax request with flashing setInterval

Asked

Viewed 193 times

2

The following piece of code aims to place the contents of a text file inside a div. Like the text file can be updated, the div is intended to be updated with the latest file content. But every 2 seconds the contents of the div blinks. How can I achieve the same obtjective without the content of the div blinking? Thank you.

<script type="text/javascript">
   setInterval("loadXMLDoc()",2000);
   function loadXMLDoc(){
        var xmlhttp;
       // codigo para IE7+, Firefox, Chrome, Opera, Safari
       xmlhttp=new XMLHttpRequest();
       xmlhttp.onreadystatechange=function(){
           document.getElementById("minhaDiv").innerHTML=xmlhttp.responseText;
       }
      xmlhttp.open("GET","ajax_doc.txt",true);
      xmlhttp.send();
   }
 </script>
  • 2

    Hi John, what you are doing is on the right track. I would change to setTimeout(loadXMLDoc, 2000); to be fired inside the .onreadystatechange to make sure you only get one call at a time. Regarding "blink" gives me the idea that you have a lot of content and that the browser needs time to render... can you change less content at a time? And by the way you can clarify in your question what has this content? Images? text only?

  • 1

    It is also necessary to check whether the responseText is already available as the onreadystatechange is called several times before that. Maybe it is the cause of the problem.

  • The content is text only. It is a. txt file with only two words. Thank you

1 answer

0


@John, I performed some tests here, the problem occurred when the innerHTML was replaced by a long text.

To get around this situation, I did the following.

  1. I created a div calling for container
  2. I added the text to div container
  3. I cleaned the div minhaDiv
  4. I added the div containerà div minhaDiv

For the demo below, I used the Baconipsum API to generate random text.

setInterval(loadXMLDoc,2000);

var minhaDiv = document.getElementById("minhaDiv");
function loadXMLDoc(){
    var xmlhttp;
    // codigo para IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.responseText && xmlhttp.responseText.length > 0)
        {   
            var content = document.createElement("div");            
            var paragrafos = JSON.parse(xmlhttp.responseText);
            for (var i = 0; i < paragrafos.length; i++) {
                var paragrafo = document.createElement("p");                
                paragrafo.innerHTML = paragrafos[i];
                content.appendChild(paragrafo);         
            }
        
            if (minhaDiv.firstChild){
                minhaDiv.removeChild(minhaDiv.firstChild);
            }
            minhaDiv.appendChild(content);
        }
    }
    xmlhttp.open("GET","http://baconipsum.com/api/?type=all-meat&paras=10&start-with-lorem=1",true);
    xmlhttp.send();
}
<div id="minhaDiv">
</div>

  • Thank you for the answer. I will study this code that I thought was quite good. Because there are traces of the code that concern the manipulation of the DOM.

  • 2

    @Tobymosque I think my comment up there might have induced you to check if the answer is empty, but it would be better to check if the request is completed and was successful: if(xmlhttp.readyState == 4 && xmlhttp.status == 200)

Browser other questions tagged

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