Home  >  Article  >  Backend Development  >  Ajax implements partial page refresh--message refresh

Ajax implements partial page refresh--message refresh

little bottle
little bottleforward
2019-04-22 11:26:583188browse

Recently, some friends were curious about the message board displaying messages that are constantly being refreshed. What is the implementation principle? Generally, sending a message means sending data to the server through normal ajax, while displaying the message in real time requires polling. This article mainly talks about using ajax to refresh the message status. It has certain reference value. Interested friends can learn about it.

What is polling: It uses a timer to initiate requests from the client to the server at fixed intervals.

Case code demo:

<script>//每间隔2秒向服务器发起请求setInterval(function(){    var xhr = new XMLHttpRequest();
    xhr.open(&#39;get&#39;,&#39;get_msg.php&#39;);
    xhr.onreadystatechange = function(){        if(xhr.readyState == 4 && xhr.status == 200){            var res = eval("("+xhr.responseText+")");
            .......
        }
    }
},2000);</script

Note: In order to prevent the message board from being overwritten by duplicate data, maxid must be marked and new data must be fetched each time

##Overall effect:

Related tutorials:

ajax video tutorial

The above is the detailed content of Ajax implements partial page refresh--message refresh. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete