const tournamentsList = document.getElementById('tournaments-name-list');
const request = new XMLHttpRequest();
request.open(
"GET",
"https://cricheroes.in/api/v1/tournament/get-tournaments/-1?pagesize=12&status=-1"
);
let objFromJson = "";
request.onreadystatechange = () => {
if (request.readyState === 4 && request.status === 200) {
objFromJson = JSON.parse(request.response)
console.log(objFromJson)
for (let i = 0; i < objFromJson.data.length; i ) {
tournamentsList.innerHTML = ` <li class="tournaments-name">${objFromJson.data[i].name}</li>`;
}
}
};
request.send();
let url = "";
url = objFromJson.page.next;
console.log(url);
I want the last three lines of code(from defining let variable to logging it out) to be executed after the execution of the callback function of onreadystatechange event. How can I achieve this?
CodePudding user response:
from readyState === 4
your execution will continue that means you got the data now you can continue your logic from there.
Your question maybe looks incomplete. If you're trying to modify some href or some src attribute then you also need to write that setAttribute
code respective to that element which you want to change. The following code will only assign value to url
const tournamentsList = document.getElementById('tournaments-name-list');
const request = new XMLHttpRequest();
let url = "";
request.open(
"GET",
"https://cricheroes.in/api/v1/tournament/get-tournaments/-1?pagesize=12&status=-1"
);
let objFromJson = "";
request.onreadystatechange = () => {
if (request.readyState === 4 && request.status === 200) {
objFromJson = JSON.parse(request.response)
console.log(objFromJson)
for (let i = 0; i < objFromJson.data.length; i ) {
tournamentsList.innerHTML = ` <li class="tournaments-name">${objFromJson.data[i].name}</li>`;
}
url = objFromJson.page.next
console.log(url);
}
};
request.send();