views:

344

answers:

1

I've got a bit of a problem with a Facebook integration.

When my users click a link, a function checks if a user is logged into Facebook. If they are not logged in, I set a piece of data, and then run the Facebook login stuff. When Facebook returns that they login, I go check the piece of data that was stored and run the original function they selected.

The problem:

The Facebook function runs twice. There are some comments on their message boards about this issue, but it seems this has been an issue since the beginning of connect, and it isn't being addressed.

So, I don't want to wait for Facebook. Is there any way for me to prevent the function from calling my function more than once?

Can I set something that says 'run once'? The problem being that once it runs once, if the user clicks again, I will need to run it again, so I can't completely disable the function (even if that were possible).

+1  A: 

This is pretty hack-ish, but it should get around the issue of Facebook calling your function twice. Since there's no code, I'm going to make some up for the purpose of my solution. Also, I'm assuming that the facebook login code has a callback (how else would it be calling your function twice?).

// This function is what is called when the link is clicked
// funToRun - the function that should be run after login is confirmed.
// params - parameters to pass to fToRun, as an array
clickWrapper = function(funToRun, params)
{
    // Keeps track of the state of the click
    var doFunction = true;

    var callback = function()
    {
        if (doFunction)
        {
            funToRun.apply(this, params);
            doFunction = false;
        }
    }

    // The facebook 
    facebookLogin(callback);
}

In this solution, you're wrapping the function that you want run in a callback that will only run once, since doFunction will prevent future callbacks from doing anything. However, it will run at least once every time the user clicks, since doFunction is managed locally (and thus is true once for each click).

Daniel Lew
yeah, that's what I ended up doing. I have a param that is set, and I check it, and then clear it if it runs the first time, so it isn't around the second time.
pedalpete