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

176
Vistas
How do I save external data to a global array?

I am presently trying to use javascript's "fetch" as to get an external file(a gist file, referenced by URL), and I have been able to have it get the intended data and display it within a console log, and have within the function getText() made the array lines work alongside innerHTML, but when i reference the array using window it doesnt seem to work, and the console comes up with an error "Uncaught TypeError: Cannot read properties of undefined (reading '0')" Does anyone have any idea why this might be happening? I think it's local/global variable issues but I'm not wholly sure. Thank you!

   function getText(){
      fetch('myfile.txt')
      .then((res) => res.text())
      .then((data)=> {
        console.log(data);
    
    var lines = [data];

  })
  .catch((err) => {
    console.log(err);
  })
}



var textPosition = 0;
var speed = 100;
typewriter = () => {
  document.querySelector("#type").innerHTML = window.lines[0].substring(0,textPosition) +"<span>\u25ae</span>";

  if(textPosition ++ != window.lines[0].length)
    setTimeout(typewriter,speed);
}
window.addEventListener("load", getText)
window.addEventListener("load", typewriter)
about 4 years ago · Juan Pablo Isaza
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