我一直在使用Javascript SDK试验Facebook API.每当我登录Facebook时,我都可以非常可靠地致电:
function init() {
// Called after loading the Facebook SDK
FB.api("/me", function(response) {
console.log(response.first_name); // "John" (always works)
});
}
Run Code Online (Sandbox Code Playgroud)
但我也希望获得一个用户相册(当他们点击一个链接时),由于某种原因,这个电话只能间歇性地工作:
$(document).ready(function) {
$("#get-albums-link").click(function() {
var accessToken = "456"; // Just an example
FB.api("/me/albums?access_token=" + accessToken, function(response) {
console.log(response); // An empty object about 40% of the time
});
});
});
Run Code Online (Sandbox Code Playgroud)
当发生这种情况我可以打开一个新的标签和验证甚至直接查询返回此相同的空结果(请注意,虽然这可能是一个授权问题,没有明显的迹象表明它是,它只是一个空的JSON对象):
<!-- An HTTPS GET at https://graph.facebook.com/me/albums?access_token=456 -->
{
"data": [
]
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码定义了一个Car.每个Car都有一个颜色,还有一个setColor(color)功能.我想添加每次调用时setColor(color)调用的侦听器函数,我希望能够随时随地处理这些侦听器函数.这是一种合适的方法吗?有更干净的方式吗?
function Car() {
this._color = 'red';
this._callbacks = {};
this.setColor = function(color) {
this._color = color;
console.log(">>> set car color to " + color);
if (this._callbacks['setColor']) {
this._callbacks['setColor']();
}
};
this.addListener = function(functionName, handler) {
if (this._callbacks[functionName]) {
var oldCallback = this._callbacks[functionName];
this._callbacks[functionName] = function() {
oldCallback();
handler();
}
} else {
this._callbacks[functionName] = function() {
handler();
}
}
};
}
var car = new Car();
car.setColor('blue');
car.addListener('setColor', function() { console.log("This …Run Code Online (Sandbox Code Playgroud)