- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我已经初始化了 fb api
window.fbAsyncInit = function() {
FB.init({
appId : '351808841561163',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
oauth: true
});
我有一个单独的 js 文件,其中包含一个函数:
function example(){
FB.api(
'/me/[namespace]:visit',
'post',
{ channel: link},
function(response) {
if (!response || response.error) {
console.log(response.error);
} else {
console.log('Follow was success! Action ID: ' + response);
}
});
}
当我调用它时,我得到 FB is undefined。
当我将函数放在 window.fbAsyncInit 中时,它工作正常,但我需要在 window.fbAsyncInit 外部调用 FB。
有没有办法做到这一点?
最佳答案
只需将您的函数排队,然后在 FB 初始化后立即调用它。以下代码保证您的函数将以正确的顺序调用,并在 FB 完成初始化后立即调用
您在示例之前和 FB 初始化脚本之前包含的帮助脚本:
var FB; // to avoid error "undeclared variable", until FB got initialized
var myQueue = new Array();
function queueAdd(f){
if (FB == undefined)
myQueue.push(f);
else
f();
}
function processQueue(){
var f;
while(f = myQueue.shift())
f();
}
您的函数示例:
function example(){
FB.api(
'/me/[namespace]:visit',
'post',
{ channel: link},
function(response) {
if (!response || response.error) {
console.log(response.error);
} else {
console.log('Follow was success! Action ID: ' + response);
}
});
}
queueAdd(example); // will run immediately if FB initialized. Otherwise, wait for FB first
和 Facebook 部分:
window.fbAsyncInit = function() {
FB.init(blablabla);
processQueue();
}
关于facebook - 如何在window.fbAsyncInit外调用FB.api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12743356/
我正在使用 Facebook 的 Javascript SDK 来允许登录/注销,我的问题是当我使用他们的 window.fbAsyncInit 调用来启动 FB session 时,它总是触发两次。
我正在从 Facebook 开发者网站上的 this page 复制代码,但 fbAsyncInit() 方法从未触发。我也读过 this page ,我用很多不同的方式调整了代码,但我无法触发该方法
在我的站点中,我使用异步加载 Facebook JS SDK。为了实际设置它,我在 window.fbAsyncInit 函数中使用了标准的 FB.init。 但问题是,在我的站点中,此功能出现在每个
(function(d, debug){ var js, id = 'facebook-jssdk', ref = d.getElementsByTagNam
fbAsyncInit 回调函数中的 FB 对象仍未定义。一旦 FB 加载并准备好使用 FB.init({}) 进行初始化,这样做的目的不就是回调吗? 如果我在 setTimeout 中添加另一个异步
我正在实现一个可以动态添加 facebook like 按钮的编辑器,所以我编写了以下代码: utils.insertFacebookSdkJs = function (win) { var
我正在使用 webpack 构建一个针对浏览器的库。我真的很想在不手动修改捆绑库的情况下复制 fbasyncinit 行为。 webpack 是否提供了在加载库本身后调用函数的方法? 或者是否有另一个
我想知道为什么它看起来像是 response.status === 'not authorized' 的“else if”情况 并且“其他”情况(“未知”,即用户未登录 facebook)未被执行。当
我在让 fbAsyncInit 在 Chrome 中触发时遇到了一些问题,但我可以看到它在 Firefox 中触发(通过警报)。 我已经阅读了一些 stackoverflow 问题/答案,发现很多可以
我是一名优秀的程序员,十分优秀!