Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8266243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:05:15+00:00 2026-06-08T05:05:15+00:00

The Facebook Graph API code I am using to login users is calling my

  • 0

The Facebook Graph API code I am using to login users is calling my getfriends() loop function twice. How can I change the code so that the loop function is only called once, whilst still allowing users in various states to login?

    <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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T05:05:18+00:00Added an answer on June 8, 2026 at 5:05 am

    It appears that the problem may be that updateButton() is run more than once and that in turn calls getfriends() more than once.

    Judging from the Facebook docs, since you set status: true in init(), you don’t need this line:

      FB.getLoginStatus(updateButton);
    

    That will happen automatically in the next line where you invoke subscribe(). So that’s probably why you’re seeing getfriends() executed twice.

    If that doesn’t work, a quick but much less elegant workaround might be to have a variable like hasfriends, initialize it to false, check its value inside of getfriends(), and have getfriends() do nothing if it’s true. If it’s false, set it to true in getfriends() so the main code in getfriends() only runs once.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using the Facebook Graph API and the PHP SDK, I know that I can
I am trying to retrieve information through Facebook graph api, using the below code
I'm developing an HTML app that integrates Facebook Graph API using Javascript SDK. Facebook
I'm getting an event using Facebook Graph API and PHP using the following code:
I am using the Facebook Graph API to get the users locale information (updated
I'm using this library for Facebook graph API access within my codeigniter project: http://www.haughin.com/code/facebook/
If you using Facebook graph API, you can see all public information of someone
I'm trying to publish checkin using Facebook Graph API. I've gone through Facebook API
I've been just starting on Facebook Graph API. How can I call several attributes
I'm using Koala for Facebook Graph API calls and am using a block for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.