I have a doing a tutorial for XMLHttpRequest but I can't seen to figure out why my data is returning null. I need to make a request to the url and return a random word. The first time the interval invokes, the html will dispaly loading... but every time after that, it is blank. Any help is appreciated
Here is a code snippet of my HTML
<div>
<h1>Data to be added</h1>
<h1 id='add-text'></h1>
</div>
Here is a snippet of my Javascript
let obj = {
isLoading: false,
load: function() {
if (this.isLoading) {
return null;
} else {
this.isLoading = true;
}
let request = new XMLHttpRequest();
let response = "loading...";
request.addEventListener("load", function(ev) {
response = ev.target.response;
this.isLoading = false;
console.log('event log', response) //logs "Random Words API" as expected
});
request.open("GET", "https://random-words-api.vercel.app/");
request.send();
return response
}
};
setInterval(function() {
let response = obj.load();
console.log('line 25', response) //logs loading... then null every time after first
document.getElementById("add-text").innerText = response;
}, 4000);