2015-05-05 138 views
2

所以我有一個基本上調用另一個函數的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; 
     }); 
    } 
+2

'show_confirmation'不返回任何東西。它在yes和no按鈕上建立事件處理程序,然後返回。那些事件處理程序在用戶點擊按鈕時返回,但是'show_confirmation'不等。 – Barmar

+0

您是否期望show_confirmation根據按鈕上的單擊返回true或false?這樣的「.click」是事件處理程序附加到DOM元素,但函數不會停止,直到它得到的東西,你可能想要在yesButton和noButton中調用一個函數,傳遞true或false,這可能是什麼你想做(或使用回調)。 – briosheje

回答

3

你應該使用一個回調:

function show_confirmation(message, height, callback) { 
    // ... 

    $('#yesButton').click(function (e) { 
     e.preventDefault(); 
     $('.confirmBox').hide("slow"); 
     callback(true); 
    }); 
    $('#noButton').click(function (e) { 
     e.preventDefault(); 
     $('.confirmBox').hide("slow"); 
     callback(false); 
    }); 
} 

show_confirmation("<some text>", "245px", function(confirmation) { 
    if (confirmation) { 
     // yes button clicked 
    } 
    else { 
     // no button clicked 
    } 
}); 
3

的解決方案是使用回調。你不能在瀏覽器中使用阻塞功能。

讓你的show_confirmation函數接受一個用返回值調用的函數參數。

1
function show_confirmation(message, height, callback) { 
     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"); 
      callback(true); 
     }); 
     $('#noButton').click(function (e) { 
      e.preventDefault(); 
      $('.confirmBox').hide("slow"); 
      callback(false);  
     }); 
    } 

function someAnotherFunction(value){ 
    if(value){ 
     //yesButton 
    }else{ 
     //noButton 
    } 
} 

用法:

show_confirmation("message", 0, someAnotherFunction); 
相關問題