从 javascript 回调函数中检索值
var display_welcome = function(){
var fb_login_button = jQuery('#fb_login_button');
var fb_welcome = jQuery('#fb_welcome');
var name = '';
FB.api('/me',function(response){
console.log(response);
name = response.first_name;
});
fb_login_button.css('display', 'none');
fb_welcome.html('<span>Welcome, ' + name + '</span>');
fb_welcome.css('display', 'block');
};
当用户从网站登录 Facebook 时会调用此函数。目标是向用户显示一条带有用户名的欢迎消息。问题是变量“name”是 FB.api() 回调方法范围内的局部变量。提取该值并将其用于我的函数“display_welcome”的最佳方法是什么?
var display_welcome = function(){
var fb_login_button = jQuery('#fb_login_button');
var fb_welcome = jQuery('#fb_welcome');
var name = '';
FB.api('/me',function(response){
console.log(response);
name = response.first_name;
});
fb_login_button.css('display', 'none');
fb_welcome.html('<span>Welcome, ' + name + '</span>');
fb_welcome.css('display', 'block');
};
This function is called when a user logs into Facebook from a website. The goal is to display a welcome message to the user with the user's first name. The problem is the variable 'name' is a local variable inside the scope of the callback method of FB.api(). What is the best way to extract this value and use it in my function 'display_welcome'?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将这些代码行移动到 API 的回调中怎么样?像这样:
var display_welcome = function(){
var fb_login_button = jQuery('#fb_login_button');
var fb_welcome = jQuery('#fb_welcome');
变量名称 = '';
};
How about moving those lines of code into the callback of the API? Like so:
var display_welcome = function(){
var fb_login_button = jQuery('#fb_login_button');
var fb_welcome = jQuery('#fb_welcome');
var name = '';
};
我会这样做:
I'd do:
将其余代码放入回调函数中。
Put the rest of your code in the callback function.
我认为最好通过订阅 FB.init() 之后的事件来获取用户信息。像这样的东西:
I think it would be best to get user info by subscribing to an event after FB.init(). Something like this:
问题是,当您调用 FB.api(...) 函数时,它是异步的,也就是说,您的代码不会停止并等待结果,它不会等待您提供的回调被调用。相反,显示欢迎消息的接下来三行代码将在回调发生之前执行,这意味着在您尝试使用时
name
变量仍为空它。解决方案是按照其他答案,即将您的欢迎消息代码移到回调函数中,这样您就不会尝试使用name
,直到它被设置:(顺便说一句,这我不喜欢一次性变量的说法:除非您在页面的其他地方使用它(大概您没有使用它,因为它是您的
display_welcome()
函数的本地函数),否则您实际上并不需要name
变量,只需在回调中直接使用response.first_name
即可。)The problem is that when you call the
FB.api(...)
function it is asynchronous, that is, your code doesn't stop and wait for the result, it doesn't wait for the callback you supply to be called. Instead the next three lines of code that display your welcome message will be executed before the callback occurs, which means thename
variable will still be blank at the point you try to use it. The solution is as per the other answers, i.e., move your welcome message code into the callback function so that you don't try to usename
until it has been set:(As an aside, and this is my dislike of single-use variables talking: Unless you are using it elsewhere on your page (which presumably you are not since it is local to your
display_welcome()
function) you don't really need thename
variable at all, just useresponse.first_name
directly within the callback.)