admin管理员组

文章数量:1399933

I am trying call an ajax request until it returns a true value. I have tried the following code but it does not return any results and no errors in the console. Any idea what I am doing wrong?

function getUserData() {
var xhr = new XMLHttpRequest();
xhr.open("GET", ".json", true);
xhr.onreadystatechange = function() {
  if (xhr.readyState == 4) {
    var resp = JSON.parse(xhr.responseText);
    return resp.status;
  }
}
xhr.send();
}

setInterval(function () {
if (getUserData() === "true") {
   alert("true");
}
}, 10000);

I am trying call an ajax request until it returns a true value. I have tried the following code but it does not return any results and no errors in the console. Any idea what I am doing wrong?

function getUserData() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://api.example./data.json", true);
xhr.onreadystatechange = function() {
  if (xhr.readyState == 4) {
    var resp = JSON.parse(xhr.responseText);
    return resp.status;
  }
}
xhr.send();
}

setInterval(function () {
if (getUserData() === "true") {
   alert("true");
}
}, 10000);
Share Improve this question asked Nov 20, 2012 at 4:40 DanielDaniel 4,34212 gold badges52 silver badges69 bronze badges 2
  • This will (try to) send an XHR once every ten seconds until the end of time. – John Dvorak Commented Nov 20, 2012 at 4:45
  • What result are you expecting ? – Shamis Shukoor Commented Nov 20, 2012 at 4:47
Add a ment  | 

3 Answers 3

Reset to default 5

getUserData calls an asynchronous function internally, so it has returned long before the AJAX call is actually finished.

Instead of doing this in a setInterval loop, you might want to try calling getUserData again in the failure case. For example:

function getUserData() {
    var xhr = new XMLHttpRequest();

    xhr.open("GET", "http://api.example./data.json", true);
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            var resp = JSON.parse(xhr.responseText);
            if (resp.status) {
                alert("true");
            } else {
                setTimeout(getUserData, 10000);
            }
        }
    }
    xhr.send();
}

getUserData();

Since you are calling it async, you can't return a value from function. Try this:

function getUserData() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://api.example./data.json", true);
xhr.onreadystatechange = function() {
  if (xhr.readyState == 4) {
    var resp = JSON.parse(xhr.responseText);
    if (resp === "true")
        alert("true");
    else
        getUserData();
  }
}
xhr.send();
}

Yes, your function is going to return immediately, but will always return undefined.

Since the operation is asynchronous you need to rewrite it to be based on a callback.

function getUserData(callback) {
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://api.example./data.json", true);
xhr.onreadystatechange = function() {
  if (xhr.readyState == 4) {
    var resp = JSON.parse(xhr.responseText);
    callback(resp.status);
  }
}
xhr.send();
}

setInterval(function () {
var isTrue = "false";

while(isTrue !== "true"){
   getUserData(function(result){ isTrue = result });
}

}, 10000);

本文标签: javascriptrepeat function until trueStack Overflow