title: Facebook login auth code calling loop function twice
body: <p>The Facebook Graph API code I am using to login users is calling my <code>getfriends()</code> 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?</p>
  
  <pre><code>    &lt;html xmlns:fb="https://www.facebook.com/2008/fbml"&gt;
  &lt;head&gt; 
  &lt;title&gt;FB App&lt;/title&gt; 
  &lt;script&gt; 
  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 &lt; l; i += 1) {
          userID = userList[i].id;
          compareFriendsWith (i, userID);
      }
  }
  function compareFriendsWith (i, id) {
      console.log("compareFriendsWith", i, id);
  }
  
  &lt;/script&gt; 
  &lt;/head&gt; 
  &lt;body&gt;
  &lt;div id="fb-root"&gt;&lt;/div&gt;
  &lt;p align="right"&gt;&lt;button id="fb-auth"&gt;Login&lt;/button&gt;&lt;/p&gt;
  &lt;script type="text/javascript"&gt;// &lt;![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);
  }());
  // ]]&gt;&lt;/script&gt;
  &lt;/body&gt; 
  &lt;/html&gt;
  </code></pre>