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

214
Views
JavaScript Array can't updating in real time

I write the following code to try to get an array from the background using XMlHttpRequest:

var temp = new Array();
var httpRequest = new XMLHttpRequest();
httpRequest.open('POST', url, true);
httpRequest.send(JSON.stringify(choose + "#" + input + "#"));
httpRequest.onreadystatechange = function() {
    var message = httpRequest.responseText;
    var temps = message.split("#");
    for (var i = 0; i < temps.length; i++)
        temp.push(temps[i]);
}
console.log(temp[0]);
console.log(temp.length);

But the first time I execute it, it returns me length=0 and temp[0]=undefined; When I execute it the second time, it returns me the correct length and temp[0],it looks like the first time didn't execute at all. What's the problem and how should I deal with it?

about 4 years ago · Juan Pablo Isaza
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!