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 2

    The technology is called commet

    There are several ways:

    1. 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.

    longpoll
    Source: learn.javascript.ru

    1. Short polls: the client part sends timer requests, if the data has changed, it renders them.

    2. 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