【发布时间】:2013-05-12 13:43:11
【问题描述】:
我有两种方法。第二个调用第一个。当我将警报函数放入第一个函数时,我可以看到返回值。但第二个函数将值视为未定义。我不明白为什么 2. 无法处理价值?
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值被忽略了,因为回调是通过事件循环异步调用的,而不是通过原始调用函数。 -
谢谢,但我在这里找不到答案。我有 2 个 getJSON 函数。 $(float).html 必须等待 getTweetReply 中的第一个
-
在“详细信息”查询中发送的
id_str是否与发送到“获取回复”查询的 ID 相同? -
一些可能对您有所帮助的背景资料:html5rocks.com/en/tutorials/async/deferred
标签: javascript jquery ajax function asynchronous