How to make a block that is updated every N seconds. This block has include "content.dat" if I change something in content.dat then this block is changed on the page without updating
|
2 answers
The technology is called commet
There are several ways:
- Long polls (longpoll): the page creates a request to the server, the server leaves this request to hang until the data remains unchanged . As soon as the data has changed, a response is sent to the request with new data, the client updates it.

Source: learn.javascript.ru
Short polls: the client part sends timer requests, if the data has changed, it renders them.
Web sockets (WebSocket): a separate protocol over http for two-way communication between the client and server.
Read more and about other ways you can read here:
https://learn.javascript.ru/ajax
https://learn.javascript.ru/xhr-longpoll
https://learn.javascript.ru/websockets
|
Plug in jQuery head
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> Then you create a block where information will be displayed, or you take an existing one.
<div id = "updateblock"> ΠΠ°Π³ΡΡΠ·ΠΊΠ° ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΠΈ, ΠΏΠ°ΡΡ ΡΠ΅ΠΊΡΠ½Π΄... </div> And then paste the script:
<script type="text/javascript"> function runMultiple() { $.ajax({ url: 'content.dat', // Π²Π°Ρ ΡΠ°ΠΉΠ» success: function(data) { $("#updateblock").html(data); // Π±Π»ΠΎΠΊ, ΠΊΠΎΡΠΎΡΡΠΉ ΠΎΠ±Π½ΠΎΠ²Π»ΡΠ΅ΠΌ } }); } var timerMulti = window.setInterval("runMultiple();", 2000); // 2000 ΠΌΠΈΠ»Π»ΠΈΡΠ΅ΠΊΡΠ½Π΄ </script> - perhaps a new version of jQuery has appeared, since I took the code from my old project, but this will not affect the workability - Nikita
- The code is working, but I have many includes in that file (content.dat) and they do not display what to do ??? - BedOmar
- if you are about php's include, then it is obvious that the file extension should be .php, not .dat - Nikita
- include this php code, judge logically on the php page (index.php) there is an inclusion that means this reduction of the size of the page itself can be replaced with the same code, so in which file format there is no difference - BedOmar
|