我有2种方法。第二个叫第一个。当我把一个警报函数放入第一个函数时,我可以看到返回值。但第二个函数将该值视为未定义。我无法理解为什么2.一个人无法处理价值?jquery函数不等Javascript结果的结果为什么?
function getTweetReply(id_str) {
$.getJSON("get_tweet_reply.php", {id_str: id_str}, function(json) {
tweet_relpy = '<blockquote>'+json.results[0].text+'</blockquote>';
alert(tweet_relpy); // --> I can see the result
return tweet_relpy;
});
}
$(document).on("click", ".tweet",function(){
var id_str = $(this).attr("id");
$.getJSON("get_tweet_details.php", {id_str: id_str}, function(json) {
tweet = '<img src="'+json.results[0].profile_image_url+'"><br>\
' + json.results[0].from_user + '<br>\
' + json.results[0].from_user_name + '<br>\
' + getTweetReply(json.results[0].id_str) + '</b><br>'; // --> undefined
$("#float").html('<div id="replybox">'+ tweet +'</div>');
});
});
你很可能是能够看到的结果,但'return'价值越来越被忽略,因为回调是通过事件循环异步调用,而不是通过原始的调用函数。 – Alnitak 2013-05-12 13:49:58
谢谢,但我无法在这里找到答案。我有2 getJSON函数。 $(float).html必须等待getTweetReply中的第一个 – 2013-05-12 13:53:31
是在“details”查询中发送的'id_str'查询与发送给“get reply”查询的ID相同的ID吗? – Alnitak 2013-05-12 13:57:56