Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!