Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

Facebook App invitation request

I want to send the App req to 5 people , i have managed to get the UserID's of 5 people but its always those same 5 people , isnt there a way to randomise the USERID's which i get from Facebook ?

<script>
var profilePicsDiv = document.getElementById('profile_pics');

FB.getLoginStatus(function(response) {


  FB.api({ method: 'friends.get' }, function(result) {
    Log.info('friends.get response', result);
   var user_ids="" ;
    var numFriends = result ? Math.min(5, result.length) : 0;
    if (numFriends > 0) {
      for (var i=0; i<numFriends; i++) {
        user_ids+= (

                          ',' + result[i] 

        );
      }
    }
    profilePicsDiv.innerHTML = user_ids;
alert(user_ids);

  });
});


 function sendRequestToRecipients() {
        var user_ids = document.getElementsByName("user_ids")[0].value;
        FB.ui({method: 'apprequests',
          message: 'My Great Request',
          to: user_ids,                        ///  How to Fill the ID's HERE ?
        }, requestCallback);
      }


</script>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I havent tested, but this should work,

FB.getLoginStatus(function(response) {
 FB.api({ method: 'friends.get' }, function(result) {
   Log.info('friends.get response', result);
   var user_ids="" ;
   var totalFriends = result.length;
   var numFriends = result ? Math.min(5, result.length) : 0;
   if (numFriends > 0) {
      for (var i=0; i<numFriends; i++) {
        var randNo = Math.floor(Math.random() * totalFriends)
        user_ids+= (',' + result[randNo]);
      }
    }
    profilePicsDiv.innerHTML = user_ids;
alert(user_ids);

  });
});

Here, in loop I generate a random no from 0 to result.length (i.e. total friends in current response) And then I use that random no to fetch random id from the given list.

Edit: (after OP asked about non-repeating randoms),

Based on your requirement, this should work...

   FB.getLoginStatus(function(response) {
     FB.api({ method: 'friends.get' }, function(result) {
       Log.info('friends.get response', result);
       var user_ids="" ;
       var totalFriends = result.length;
       var randNo = Math.floor(Math.random() * totalFriends);
       var numFriends = result ? Math.min(5,totalFriends) : 0;
       if (numFriends > 0) {
          for (var i=0; i<numFriends; i++) { 
            user_ids+= (',' + result[randNo]);
            randNo ++;
            if(randNo >= totalFriends){
                randNo = 0;
            } 
          }
        }
        profilePicsDiv.innerHTML = user_ids;
    alert(user_ids);

      });
    });

Here, instead of generating random no each time, I generate a random no once and then increment it. If random no exceeds the total no of friends, then I start from 0.

This will always give random friends each time :)

Code is not tested, I apologize if has errors ( But code surely gives you a direction to think)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...