Answer the question
In order to leave comments, you need to log in
How to competently organize a page refresh to send a request?
There is a page on which, using php, a code is written that checks the last message in VK and responds to it under certain conditions. What is the best way to organize a page update, since the code only works when the page is updated. It is necessary that the site is somehow updated in the background if a new message arrives. The main thing is that there would not be a large load on the server. You can not paint the solution completely, but simply direct it in the right direction. Thank you very much in advance)
Answer the question
In order to leave comments, you need to log in
Fragment HTML
<body onload="(function() {getVSD();window.setInterval(getVSD, 3000)})()">
function getVSD(){ //Get Status of Servers
var xmlhttp = getXmlHttp();
xmlhttp.open('GET', 'status.php', true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if(xmlhttp.status == 200) {
var response = JSON.parse(xmlhttp.responseText);
response.forEach(function(item){loadContent(item.element,item.status);});
} else {GLOBAL_STATUS.forEach(function(item){loadContent(item.element,item.status);});}
};}
xmlhttp.send(null);
}
if(xmlhttp.status == 200)
), the corresponding info on the page changes without reloading the page as such.
To get a message from the server without refreshing the page, it's enough to make an AJAX request (HTTP request using JavaScript), you can start with jQuery or from here , but in your case you will have to send requests very often, this is a big load on both the client and the server, you can use long polling instead of frequent, but WebSockets is better, it's almost "raw" TCP / IP, unlike HTTP, you don't have to send requests (packets) at all, so there will be no load on the client and server until the server itself sends a message .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question