所以我对这些承诺很新。我很难在他们周围缠着我的头。我看了很多内容和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