Richard
Richard

Reputation: 4546

How can I make a function act like a callback

How can I make shwOpts.show("fast"); to act when removeHighlight() is done?

I thought if I put an anonymous function as an argument in the other it will act as a callback. This didn't work.

removeHighlight : function(f) {
        // remove previous highlight.
        var highlight = $('#openid_highlight');
        if (highlight) {
            highlight.replaceWith($('#openid_highlight a')[0]);
        }
    },


 moreOptsLink = $("#more-options-link").click(function () {
              moreOptsLink.detach();
              openid.removeHighlight(function(){$("#show-more-options").show("fast");});
              //shwOpts.show("fast");
              openid.setPref("showMoreOpenIdOptions", !0)
            });

Upvotes: 1

Views: 111

Answers (3)

sedran
sedran

Reputation: 3566

You should call the callback in the removeHighlight function like this:

removeHighlight : function(f) {
    // remove previous highlight.
    var highlight = $('#openid_highlight');
    if (highlight) {
        highlight.replaceWith($('#openid_highlight a')[0]);
        if( typeof f === 'function' ) {
            f();
        }
    }
},


moreOptsLink = $("#more-options-link").click(function () {
     moreOptsLink.detach();
     openid.removeHighlight(function(){
         $("#show-more-options").show("fast");
     });
     openid.setPref("showMoreOpenIdOptions", true);
});

By using typeof control, you don't take any error when you don't pass a function as parameter to the function.

Upvotes: 1

yoelp
yoelp

Reputation: 1631

removeHighlight : function(f) {
        // remove previous highlight.
        var highlight = $('#openid_highlight');
        if (highlight) {
            highlight.replaceWith($('#openid_highlight a')[0]);
        }
   //you need to call f
       f()

    },

Upvotes: 0

Andreas
Andreas

Reputation: 21911

You're not executing the callback function

removeHighlight : function(f) {
    // remove previous highlight.
    var highlight = $('#openid_highlight');
    if (highlight) {
        highlight.replaceWith($('#openid_highlight a')[0]);
    }

    if (typeof(f) === "function") {
        f();
    }
},

Upvotes: 6

Related Questions