所以我有一個基本上調用另一個函數的JavaScript函數。這個其他函數返回true或false,然後我有一個if語句,但是函數不會等待返回值,它只是繼續瀏覽代碼。圍繞這個的解決方案是什麼?函數調用另一個函數,但不等待返回值?
所以我的第一個功能有:
confirmmation = show_confirmation("<some text>", "245px");
if (confirmmation) {
return true;
}
else {
return false;
}
和調用:
function show_confirmation(message, height) {
var contentPosition = $('.content').position();
var contentHeight = $('.content').height();
var bottomPosition = contentPosition.top + contentHeight;
$('.confirmBox').css("top", (bottomPosition - ($('.confirmBox').outerHeight()+100)) + "px");
$('.confirmBox').css("left", (($('.content').width() - $('.confirmBox').outerWidth())/2) + $('.content').scrollLeft() + "px");
$('.confirmBox').css("height", height);
$('.confirmBox .confirmationMessage').html(message)
$('.confirmBox').css("display", "block");
$('#yesButton').click(function (e) {
e.preventDefault();
$('.confirmBox').hide("slow");
return true;
});
$('#noButton').click(function (e) {
e.preventDefault();
$('.confirmBox').hide("slow");
return false;
});
}
'show_confirmation'不返回任何東西。它在yes和no按鈕上建立事件處理程序,然後返回。那些事件處理程序在用戶點擊按鈕時返回,但是'show_confirmation'不等。 – Barmar
您是否期望show_confirmation根據按鈕上的單擊返回true或false?這樣的「.click」是事件處理程序附加到DOM元素,但函數不會停止,直到它得到的東西,你可能想要在yesButton和noButton中調用一個函數,傳遞true或false,這可能是什麼你想做(或使用回調)。 – briosheje