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
3 Answers
Reset to default 5getUserData
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
版权声明:本文标题:javascript - repeat function until true - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744210919a2595413.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论