Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

142
Vistas
reading and displaying a text from a file line by line using javascript on html page

I am just learning javascript, so I need your help. I have a text file, like following;

Text1
Text2
Text3
.....
.....
Textn

I would like to read all the content from the file line by line, so text by text, and display on html page one by one. One text will blink, disappear, and then next text will blink, disappear, etc. When it comes to the end, then it will start from the beginning of the file, and continue again line by line.

How can I do it?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can do it this way:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Example</title>
</head>

<body>
  <h1 id="my-element">Value</h1>

  <script>
    var target = document.getElementById("my-element");

    var content = null;
    var xhr = new XMLHttpRequest();

    xhr.open("GET", "content.txt");
    xhr.onload = () => {
      content = xhr.response.split(/\r?\n/);
      let counter = 0;
      setInterval(() => {
        target.innerHTML = content[counter++ % content.length];
        setTimeout(() => target.innerHTML = "", 500);
      }, 1000);
    };
    xhr.send();

  </script>
</body>

</html>

But note that it doesn't work using file protocol and you should run a local server to use it.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda