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

143
Visualizações
Update content from XMLHTTPRequest every 20 seconds

First of all, I would like to say that I'm a student learning programming for around a month, so expect to see many mistakes.

I'm working on a website where I use a chart from the ChartJs library. The data used for this chart is taken through requests to a server.

What I want to do is update the content of the studenGesamt variable every 20 seconds. My main idea was using a setInterval, but it didn't work. I am thinking that I could make a new request to the server every 20 seconds, but I am kind of lost on how to do that or if it is actually a good idea. If someone could help me I would really appreciate it!

let serverData;
let studenGesamt;
let date;
const http = new XMLHttpRequest();
const url = 'https://url.com/'; // I have hidden this URL as it is the actual server from my company
http.open("GET", url);
http.setRequestHeader('key', 'sample-key'); // I have hidden this key for the same reason as above
http.send();
const chart = document.getElementById("multie-pie-chart");

// Function that calculates the workdays passed up until today
const workdaysCount = () => [...new Array(new Date().getDate())]
  .reduce((acc, _, monthDay) => {
    const date = new Date()
    date.setDate(1 + monthDay) ![0, 6].includes(date.getDay()) && acc++
      return acc
  }, 0)

http.onload = (e) => {

  // Parsing the JSON file and storing it into a variable (Console.Log() to make sure it works)
  serverData = JSON.parse(http.responseText);
  console.log(serverData);

  // Storing the value of total hours from the database in a variable
  studenGesamt = serverData.abzurechnen.gesamt;

  chartRender(); // Function that calls and renders the chart

  setInterval(dataLoop, 5000); // 5 seconds to be able to test it, will be changed to 20 when finished

};

let dataLoop = () => {
  studenGesamt = serverData.abzurechnen.gesamt;
  console.log('test'); // Logging test to see if it is working
};

about 4 years ago · Juan Pablo Isaza
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