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 7995351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:23:19+00:00 2026-06-04T14:23:19+00:00

for (var i = 0; i < friends.length; i++) { var friend = $(<li>

  • 0
for (var i = 0; i < friends.length; i++) {
    var friend = $("<li>" + friends[i].name + "</li>");
    FB.api('/' + eventId + '/invited/' + friends[i].id, 'post', {}, function (response) {
        //friend here is the last one created for all callbacks
    });
}

I have a loop which invites friends to an event, I am finding the the jquery friend object created here is always the last one created for all callbacks. I am assuming this is because the Facebook call is asynchronous. How would I be able to maintain scope here?

  • 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-04T14:23:22+00:00Added an answer on June 4, 2026 at 2:23 pm

    Try this:

    for (var i = 0; i < friends.length; i++) {
        var friend = $("<li>" + friends[i].name + "</li>");
        var callback = (function(friend){
           return function (response) {
              //friend here should be the correct one
           })(friend);
        }
        FB.api('/' + eventId + '/invited/' + friends[i].id, 'post', {}, callback);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello friends here's my code function dropItems(idOfDraggedItem,targetId,x,y) { var html = document.getElementById('dropContent').innerHTML; if(html.length<=0){ html.innerHTML='<img
var query1 = urlencode($('input[name=searchTerm]').val()); //user1 $.getJSON('http://twitter.com/friends/ids.json?screen_name='+ query1 +'&jsoncallback=?', function(data){ $('content').append(''+data.length+''); }); This is a
var facebook_info = [{name:,url:}]; for(i=0;i<10;i++){ var randomNum = Math.floor(Math.random() * friend_list[0].fql_result_set.length); facebook_info[i].name=friend_list[0].fql_result_set[randomNum].name; facebook_info[i].url=friend_list[0].fql_result_set[randomNum].pic_square_with_logo; console.log(facebook_info.name[i]);
Here is some trouble with Facebook API we are facing from last monday. We
var ref1 = new Firebase(http://gamma.firebase.com/myuser/123,456); ref1.set(123,456); var on1 = ref1.on(value, function(snapshot) { console.log(snapshot.val()); });
var Foo = Foo || {}; Foo.Controller = (function ($) { var $page =
var JavascriptHelper = Backbone.Model.extend(JavascriptHelper, {}, // never initialized as an instance { myFn: function()
Complete Flash / AS Noob here. Friend wanted a change in Email address and
I am trying to store my friends list (name, picture and gender) on a
Friends, I've one question to ask. Which one is the correct syntax in jquery

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.