我的問題是在代碼中更好地解釋:的Javascript異步回調
//This code is triggered before ajax ObBegin. But I need f1 to return a boolean to either cancel or continue the event.
f1();
function f1(){
$.modalWindow.Open(); //This is an async method, this is where my problem lies.
//I need to freeze here and wait on a return value from one of the events below.
}
//In the modal window:
//An event which waits for the click event
$('.cancelBtn').click(function(){
//How do I send false back to f1?
closeModalWindow();
});
$('.yesBtn').click(function(){
//How do I send true back to f1?
closeModalWindow();
});
所以基本上會發生什麼情況是這樣的:
openModalWindow()
打開一個按鈕上點擊等待一個模態窗口。- 我想將值傳回給f1並返回它。
有沒有辦法解決這個問題?
你意識到'f2'實際上並不返回任何東西? – 2011-04-06 02:53:10
@Matt Ball,我知道,因爲我不知道如何從異步函數返回某些東西。 – 2011-04-06 03:16:53
你不這樣做 - 你運行一個回調函數(像@ Alex的回答)或者像'jQuery.Deferred'(我的回答)這樣的發燒友。但是你的問題還不清楚 - 你能告訴我們更多的代碼嗎? – 2011-04-06 03:35:48