How to fread() fetch data every 30 seconds without refreshing the page

Asked

Viewed 50 times

0

I’m wanting that function fread() or file_get_contents() search for data in the file every 30s, but without refreshing the page, I tried and couldn’t get how to do it ?

1 answer

1


Use ajax to learn more read this document of Mozilla in Portuguese

var xmlhttp = new XMLHttpRequest();
// Mude o valor da variavel url.
var url = "https://api.github.com/users?";

function get(url) {
  xmlhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
          var dadosRecebidos = JSON.parse(this.responseText);
          var out = "";
          out += '<a href="' + dadosRecebidos[0].url + '">' + dadosRecebidos[0].login + '</a><br>';
          document.getElementById("resultado").innerHTML = out;
      }
  };
  xmlhttp.open("GET", url, true);
  xmlhttp.send();
  // 30000 milisegundos = 30 segundos
  setTimeout(function(){
    get(url);
  }, 30000);
}
get(url);
<div id="resultado"></div>

  • It’s not working not

  • I tried, but it didn’t work

  • Edit your question and put your html and php code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.