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)