Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

239
Visualizações
JavaScript code to read the latest values from a json file that keeps getting updated every few seconds

I am very new to JavaScript and trying to display the latest values from a .json file, that keeps getting updated every 10 seconds, into a HTML page. I am reading the file every 10 seconds as well, but I am not getting the latest values from the file. I am getting values which are 50 to 60 seconds older.

I have tried disabling the browser cahche in chrome but still the same issue. Can somebody please help on this?

My JavaScript code to read the json file is as follows:

function readJsonFile(callback) {
 var request = new XMLHttpRequest();
 request.open('GET', 'http://localhost:8080/PersonalProject/filename.json');
 request.onload = function() {
  if(request.status>=100 && request.status<=100) {
   //perform some operation
   callback(request.responseText);
  }
  else{
   //perform some operation
   callback('FAILED');
  }
 };
 request.onerror = function() {
  console.error('Connection error');
 };
 request.send();
}


setInterval(function(){
 readJsonFile(function(stat){
  console.log(stat);
 });
}, 10000);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can add a timestamp as a parameter in your request to avoid the navigator cache:

...
let ms = Date.now();
request.open('GET', 'http://localhost:8080/PersonalProject/filename.json?ms' + ms);
...

I do that any time i need to update js and css files when requested from the html header and it works

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda