thednp
thednp

Reputation: 4479

jQuery: How can I know I am calling the right callback inside a function with multiple callback functions?

I have a function like this


$.fn.myFunction = function( options, special, callback ) {
    var settings = $.extend({
        opacity     : '',
        margin  : ''
    }, options );

    //while executing this main function, some cool things happen
    if ( special && typeof special === "function" && things_are_running) { 
        special();
    }

    //when finished executing the main function, there is one more thing
    if ( callback && typeof callback === "function" && things_stopped) { 
        callback();
    }
}

and I do some funny stuff like this

$('.selector').myFunction( options, function() { /* this is the question about*/ } );

How can I know if I am calling the special() or the callback() function considering only one callback is given?

I should do stuff like this?

$('.selector').myFunction( options, function() {}, null );

OR THIS?

$('.selector').myFunction( options, null, function() {} );

Upvotes: 0

Views: 37

Answers (1)

Barry
Barry

Reputation: 3723

Yes, but for your first example you can also use

$('.selector').myFunction( options, function() {});

The function will be assigned to special, in

if ( callback && typeof callback === "function" && things_stopped) {

callback will be evaluated as false.

Upvotes: 1

Related Questions