사용자 로그인에 사용중인 Facebook 그래프 API 코드가 내 getfriends()
루프 함수를 두 번 호출합니다. 루프 기능이 한 번만 호출되도록 코드를 변경하는 방법은 무엇입니까? 여러 상태의 사용자가 여전히 로그인 할 수 있도록하려면 어떻게해야합니까?Facebook 로그인 인증 코드가 루프 함수를 두 번 호출합니다.
<html xmlns:fb="https://www.facebook.com/2008/fbml">
<head>
<title>FB App</title>
<script>
var userList = [];
userCount = 0;
function getfriends() {
console.log("getFriends");
var url = "/me/friends";
FB.api(url, function (response) {
userList = userList.concat(response.data);
userCount = response.data.length;
compareAllFriends();
});
}
function compareAllFriends() {
console.log("compareAllFriends");
var i = 0, l = 10, userID;
for (; i < l; i += 1) {
userID = userList[i].id;
compareFriendsWith (i, userID);
}
}
function compareFriendsWith (i, id) {
console.log("compareFriendsWith", i, id);
}
</script>
</head>
<body>
<div id="fb-root"></div>
<p align="right"><button id="fb-auth">Login</button></p>
<script type="text/javascript">// <![CDATA[
window.fbAsyncInit = function() {
FB.init({ appId: 'APP_ID',
status: true,
cookie: true,
xfbml: true,
oauth: true});
function updateButton(response) {
var button = document.getElementById('fb-auth');
if (response.authResponse) {
//user is already logged in and connected
FB.api('/me', function(response) {
getfriends();
button.innerHTML = 'Logout';
});
button.onclick = function() {
FB.logout();
};
} else {
//user is not connected to your app or logged out
button.innerHTML = 'Login with Facebook';
button.onclick = function() {
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me');
} else {
//user cancelled login or did not grant authorization
}
}, {scope:''});
}
}
}
// run once with current status and whenever the status changes
FB.getLoginStatus(updateButton);
FB.Event.subscribe('auth.statusChange', updateButton);
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol
+ '//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
// ]]></script>
</body>
</html>
덕분에, 나는 그 라인을 제거하고 내가 너무 빨리 말을 지금 – Edward
작동합니다. 해당 줄을 제거하면 문제가 해결되었지만 아직 Facebook에 로그인하지 않은 사용자는 응용 프로그램에 로그인 할 수 없습니다. 그 라인이 없으면 로그인 버튼이 작동하지 않기 때문에 Facebook에 먼저 로그인해야합니다. – Edward