Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jQuery延迟承诺_Jquery_Promise - Fatal编程技术网

使用jQuery延迟承诺

使用jQuery延迟承诺,jquery,promise,Jquery,Promise,因此,我对这些承诺很陌生。我很难把我的头缠在他们身上。我看了很多图坦卡蒙之类的帖子,但没有看到像我这样的情况。 这是我目前的代码 $("#PayNow, input[name^='schedulepmt']").on("blur", function(){ var diff = 0; diff = calcBalanceDue(); if(diff){ $.when(confirmNewPayment(diff)).then(function(){ if($(thi

因此,我对这些承诺很陌生。我很难把我的头缠在他们身上。我看了很多图坦卡蒙之类的帖子,但没有看到像我这样的情况。 这是我目前的代码

$("#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()


confirmNewPayment函数应返回一个承诺:

function confirmNewPayment(diff){
    var deferred = $.Deferred();

    // Your async action here
    (function() {
        // $when will run your function when the deferred object is resolved
        deferred.resolve(diff * 2);
    })();

    return deferred.promise();
}

有关更新:

如果bootbox通过设计回报承诺,一切都会变得更容易。所以在这种情况下你可以

function confirmNewPayment(diff){
    var deferred = $.Deferred();

    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}));

                    deferred.resolve('schedule');
                }
            },
            collect: {
                label: "Mark balance as \"To Collect\"",
                className: "btn-primary",
                callback: function() {
                    $("#BalanceDueRow").removeClass("hide");
                    $("#BalanceDue").val(diff);

                    deferred.resolve('mark');
                }
            }
        }
    });

    return deferred.promise();
}


confirmNewPayment
看起来像什么?你为什么打电话给
$。什么时候打电话给它?它还承诺吗?我加上了fn。不,它不会返回任何东西。这就是为什么它不能像你期望的那样工作。你为什么要在这里使用承诺?
confirmNewPayment
的哪个部分是异步的?您希望
。然后
什么时候运行?对话框打开后?单击按钮后?您不需要
$。当
执行此操作时。您只需执行
confirmNewPayment(diff.),然后执行(function(){})我的代码应该放在
(function(){
之前,或者放在它里面?现在对它有了更好的理解,虽然它没有按预期工作,但我认为这是因为我需要
resetPmtInfo()
里面的另一个承诺,它也有一个引导框。
function confirmNewPayment(diff){
    var deferred = $.Deferred();

    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}));

                    deferred.resolve('schedule');
                }
            },
            collect: {
                label: "Mark balance as \"To Collect\"",
                className: "btn-primary",
                callback: function() {
                    $("#BalanceDueRow").removeClass("hide");
                    $("#BalanceDue").val(diff);

                    deferred.resolve('mark');
                }
            }
        }
    });

    return deferred.promise();
}
$("#PayNow, input[name^='schedulepmt']").on("blur", function(){
    ....
    confirmNewPayment(diff).then(function(selection) {
        // selection is 'schedule' or 'mark'
    });
}