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('get','get_msg.php'); 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:
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!