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

161
Visualizações
how to refresh html page to update every time websocket sends data

I want to update a simple html frontend with data coming in sporadically from the server via a websocket (I'm using socket.io). For each data, I want to create a <p> element with text inside it about the data, and append it as a child element inside <div>. Then I want the browser to automatically refresh the page to show it.

The backend is sending the data as it should when I logged it to the console. However, my frontend isn't rendering correctly. After data is sent, the browser doesn't refresh, thus not showing the <p> element. However, when I manually refresh it, the first <p> element would show and blink really fast. It does not show additional <p> elements as data is coming in.

Here is the frontend code in question:

    <div id="names"></div>

    <script src="/socket.io/socket.io.js"></script>
    <script>
      let socket = io();
      let nameDiv = document.getElementById('names');

      socket.on('message', data => {
        let newName = document.createElement('p');
        newName.textContent = data.name;
        nameDiv.appendChild(newName);
        location.reload();
      });
    </script>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

remove location.reload() from your code, then try again. You don't need to "refresh" in order for the DOM to reflect the changes you have made, the event loop will render the changes immediately.

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