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

177
Visualizações
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 à 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