Home > Backend Development > PHP Tutorial > Ajax implements partial page refresh--message refresh

Ajax implements partial page refresh--message refresh

little bottle
Release: 2023-04-06 08:18:01
forward
3289 people have browsed it

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
Copy after login

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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template