简体   繁体   中英

Callback function is executed right away

I have a confirm popup that ask for yes or no, this is the code:

Confirm({
    Title: 'Warning',
    Message: 'Are you sure?',
    Buttons: [{
        text: 'Yes',
        callback: function(){
            RRSubmitRemoveReportRequest($(this));
        },
        highlighted: true
    }, {
        text: 'No)',
        callback: null,
        type: 'no'
    }]
});

If if send the parameter $(this), I should use an anonymous function, otherwise it calls the function right away, anybody can explain me this please? Thanks

It's easy to understand with an example:

function foo(i){
    return i*10;
}

var x = foo(1); // execute foo with 1 parameter;

var x = function(){ // creates a callback to the foo function.
    foo(1);
};

var x = foo; // pointer to foo function and then:
x(1);

Bottom line, a callback should be a function that will be invoked somewhere in the future, not a value of a function.

The callback property needs to be set to a function .

If you do:

callback: RRSubmitRemoveReportRequest($(this))

you are setting callback to the return value of the RRSubmitRemoveReportRequest function.

You need to pass it a function. RRSubmitRemoveReportRequest is a function, RRSubmitRemoveReportRequest($(this)) is a function call, so it's ran and its return value is used.

When you do:

callback: function(){
    RRSubmitRemoveReportRequest($(this));
}

you are passing a function, that when called, will call RRSubmitRemoveReportRequest correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM