[英]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? 如果发送参数$(this),我应该使用一个匿名函数,否则它将立即调用该函数,有人可以向我解释一下吗? 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
. callback
属性需要设置为function
。
If you do: 如果您这样做:
callback: RRSubmitRemoveReportRequest($(this))
you are setting callback
to the return value of the RRSubmitRemoveReportRequest
function. 您正在将
callback
设置为RRSubmitRemoveReportRequest
函数的返回值 。
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. RRSubmitRemoveReportRequest
是一个函数, RRSubmitRemoveReportRequest($(this))
是一个函数调用,因此它已运行并使用了其返回值。
When you do: 当您这样做时:
callback: function(){
RRSubmitRemoveReportRequest($(this));
}
you are passing a function, that when called, will call RRSubmitRemoveReportRequest
correctly. 您正在传递一个函数,该函数在被调用时将正确调用
RRSubmitRemoveReportRequest
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.