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

140
Views
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 answers
Answer question

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 Report
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!