Javascript 检查返回的JSON的值,如果为false则更新
我有一个从外部API获取问题的函数。我需要在一个数组中至少有5个问题,我的应用程序才能正常运行Javascript 检查返回的JSON的值,如果为false则更新,javascript,jquery,Javascript,Jquery,我有一个从外部API获取问题的函数。我需要在一个数组中至少有5个问题,我的应用程序才能正常运行 function getCategoryQuestions(){ var questions = {}; var i = 0; for (i; i < 6; i++){ $.getJSON(makeURL(), function(data){ data = data.category; questions[data.name] = data.quest
function getCategoryQuestions(){
var questions = {};
var i = 0;
for (i; i < 6; i++){
$.getJSON(makeURL(), function(data){
data = data.category;
questions[data.name] = data.questions;
validQuestionsLength(questions, data.questions)
})
}
return questions;
}
函数getCategoryQuestions(){
var问题={};
var i=0;
对于(i;i<6;i++){
$.getJSON(makeURL(),函数(数据){
data=data.category;
问题[data.name]=data.questions;
有效问题长度(问题、数据、问题)
})
}
回答问题;
}
我运行validQuestionsLength检查每个问题数组的长度是否至少为5个元素
function validQuestionsLength(hash, array){
while (array.length < 5) {
$.getJSON(makeURL(), function(data){
data = data.category;
hash[data.name] = data.questions
array = data.questions;
})
console.log(array.length)
}
}
函数有效性问题长度(散列,数组){
while(数组长度<5){
$.getJSON(makeURL(),函数(数据){
data=data.category;
hash[data.name]=data.questions
数组=数据。问题;
})
console.log(array.length)
}
}
我想检查数组的长度是否小于5,如果小于5,我将从url获取一个新资源。我将把新值保存到数组中,然后它将检查这个新值的数组长度。我希望它继续提取数据,直到条件为真
所发生的事情是,一旦validQuestionsLength()方法运行,并且我的数组长度小于5,它就会保持console.logging在无限循环中记录当前数组。我以为我正在为每个循环上的数组保存一个新资源
我不确定我的逻辑错误发生在哪里
我知道发生了一个异步请求,因此在重置数组变量之前,console.log可能会运行。我编辑了我的验证函数,以包含一个完成的回调,以便在异步完成后执行
function validQuestionsLength(hash, array){
if (array.length < 5) {
$.getJSON(makeURL(), function(data){
data = data.category;
hash[data.name] = data.questions
console.log("data is ", data.questions)
array = data.questions;
}).done(console.log("array is ", array ))
}
}
函数有效性问题长度(散列,数组){
if(数组长度<5){
$.getJSON(makeURL(),函数(数据){
data=data.category;
hash[data.name]=data.questions
console.log(“数据是”,数据是问题)
数组=数据。问题;
}).done(console.log(“array is”,array))
}
}
我的意图是将新获取的数据保存到数组中。但是data.questions和数组(应该指向data.questions的值)是不同的值。.done()在这种情况下做了什么?Edit2:我举例说明了两个函数可能看起来是有效的回调循环,这两个函数都是相似的,因此只能是一个函数: 我简化了我的脚本,对于测试,我让testResponse php脚本多次返回4个元素[“1”、“2”、“3”、“4”]数组,最后返回5个元素[“1”、“2”、“3”、“4”、“5”]数组 还有很多工作要做,比如ajax错误处理 Edit:getJSON是异步函数,因此它将在获取数据后调用其回调函数,因此回调将在稍后执行,即使它未添加到事件队列中 这是因为getJSON成功回调函数执行被添加到事件队列中,并且它在您的console.log您的array.length之后执行 我做了一个测试:
<html>
<head>
<script
src="https://code.jquery.com/jquery-2.2.4.min.js"
integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
crossorigin="anonymous"></script>
</head>
<body>
<script>
$.getJSON('http://localhost/testResponse.php', function(data){
console.log('Callback');
});
console.log('Script end');
</script>
Hello!
</body>
</html>
为了进一步解释它是如何发生的,我将提供两个例子
第一个例子:
函数callCallback(回调){
回调();
}
callCallback(函数(){
log('Callback');
});
log('scriptend')代码>那么您的电话是一次返回一个问题还是N?您是在寻找每个data.name五个,还是在所有data.name实体中总共五个?现在,每个调用都会覆盖它数组的方式,因此除非api调用返回五项,否则将永远循环。使用array.push(data.questions)
而不是array=data.questions
我不知道您的Json结构,您可能需要在那里添加一些索引,如问题[0]
或其他一些东西您设置的done回调函数错误,您需要将代码嵌入lambda函数中以获得成功回调,现在您将console.log的结果设置为done事件的回调。所以它现在仍然在回调之前执行。您的while循环也不正确。您不能循环getJSON,因为您将在回调之前运行许多请求,甚至在执行第一个请求之前。你需要重新考虑整个过程,并在回调或其他方式中调用函数本身。很难确切地理解你想要得到什么结果。请添加一个或多个符合标准的问题的示例。
getCategoryQuestions(function(questions){
console.log(questions);
});
<html>
<head>
<script
src="https://code.jquery.com/jquery-2.2.4.min.js"
integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
crossorigin="anonymous"></script>
</head>
<body>
<script>
$.getJSON('http://localhost/testResponse.php', function(data){
console.log('Callback');
});
console.log('Script end');
</script>
Hello!
</body>
</html>
Script end
Callback