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

141
Visualizações
How to execute some statements after the execution of a function in javascript
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?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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();
about 4 years ago · Juan Pablo Isaza Relatório
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