所以我對這些承諾很新。我很難在他們周圍纏着我的頭。我看了很多內容和SO帖子,但沒有遇到類似我的情況。 這是我目前的代碼。使用jQuery推遲承諾
$("#PayNow, input[name^='schedulepmt']").on("blur", function(){
var diff = 0;
diff = calcBalanceDue();
if(diff){
$.when(confirmNewPayment(diff)).then(function(){
if($(this).attr("id") === "PayNow" && input[name^='schedulepmt'].length && ($(this).val() !== $(this).data("prevValue"))){
if(!resetPmtInfo()) {
$(this).val($(this).data("prevValue"));
}
}
});
}
});
我想.then()
裏面的代碼confirmNewPayment()
已執行後執行。它似乎仍然在運行異步。
謝謝。
這裏是confirmNewPayment()
function confirmNewPayment(diff){
bootbox.dialog({
message: "There is an outstanding balance. How would you like to proceed?",
title: "Outstanding Balance Options",
closeButton: false,
buttons: {
addpmt: {
label: "Add a scheduled CC payment",
className: "btn-success",
callback: function() {
scheduledPmtData.PaymentCount += 1;
$("#ScheduledPayments").append($("#ScheduledPmtTemplate").render(scheduledPmtData,{amount:diff}));
}
},
collect: {
label: "Mark balance as \"To Collect\"",
className: "btn-primary",
callback: function() {
$("#BalanceDueRow").removeClass("hide");
$("#BalanceDue").val(diff);
}
}
}
});
}
是什麼'confirmNewPayment'樣子?你爲什麼要調用'$ .when'呢?它是否會返回一個承諾? – 2014-10-08 19:52:18
我加了fn。不,它不會返回任何東西。 – dbinott 2014-10-08 19:55:42
這就是爲什麼這不像你期望的那樣工作。你爲什麼想在這裏使用承諾? confirmNewPayment'的哪一部分是異步的?你想什麼時候運行'.then'?對話框打開後?點擊一個按鈕後? – 2014-10-08 19:56:18