[英]Callback function is executed right away
我有一個確認彈出窗口,詢問是或否,這是代碼:
Confirm({
Title: 'Warning',
Message: 'Are you sure?',
Buttons: [{
text: 'Yes',
callback: function(){
RRSubmitRemoveReportRequest($(this));
},
highlighted: true
}, {
text: 'No)',
callback: null,
type: 'no'
}]
});
如果發送參數$(this),我應該使用一個匿名函數,否則它將立即調用該函數,有人可以向我解釋一下嗎? 謝謝
用一個例子很容易理解:
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);
最重要的是,回調應該是將來會在某處調用的函數 ,而不是函數的值。
callback
屬性需要設置為function
。
如果您這樣做:
callback: RRSubmitRemoveReportRequest($(this))
您正在將callback
設置為RRSubmitRemoveReportRequest
函數的返回值 。
您需要將其傳遞給函數。 RRSubmitRemoveReportRequest
是一個函數, RRSubmitRemoveReportRequest($(this))
是一個函數調用,因此它已運行並使用了其返回值。
當您這樣做時:
callback: function(){
RRSubmitRemoveReportRequest($(this));
}
您正在傳遞一個函數,該函數在被調用時將正確調用RRSubmitRemoveReportRequest
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.