jQuery延迟链接问题 [英] jQuery deferred chaining problems

查看:93
本文介绍了jQuery延迟链接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我可能会遗漏一些显而易见的东西,尽管我试图找到一个类似的例子,但我找不到想要的例子.我需要一系列ajax调用以特定顺序运行.我正在使用以下代码完成交易:

Ok, I am probably missing something obvious, and although I've tried to find a similar example, I can't find one quite like what i'm wanting to do. I'm needing a series of ajax calls to run in a particular order. I'm using the following code to finalize a transaction:

showStandbyDialog();
    $.when(function(){console.log('Paying Charges due before transaction');})
        .always( this.applyCredit(parseFloat($(this.currentChargesTarget).html())) ) // Pay charges due before transaction
        .always(function(){console.log('Applying renewals');})
        .always( this.applyRenewals() ) // Apply Renewals
        .always(function(){console.log('Paying renewal charges');})
        .always( this.applyCredit(this.renewCart.length * this.renewCost) ) // Pay renewal charges
        .always(function(){console.log('Applying checkouts');})
        .always( this.applyCheckOut() ) // Apply checkouts
        .always(function(){console.log('Paying checkout charges');})
        .always( this.applyCredit(this.cart.length * this.checkOutCost) ) // Pay checkout charges
        .always(function(){console.log('Applying card replacement');})
        .always( this.applyCardReplacement() ) // Apply card replacement
        .always(function(){console.log('Paying leftover charges');})
        .always( this.applyCredit(this.cardCost) ) // Pay leftover charges
        .always(function(){console.log('Finalizing Transaction');})
        .always( function(){ updateCharges(); bfwd.Patron.Transaction.reset(); hideStandbyDialog(); } ); // Reset Transaction and clear standby dialog

现在我已经尝试了.done,.then和几乎任何.anything(),但是this.applyCredit()句柄函数中的console.log()代码总是在console.log('Finalizing Transaction ').每个this.function()调用都会返回一个jquery延迟方法,以防万一.

Now I have tried, .done, .then, and just about .anything() but the console.log() code in the handle function of this.applyCredit() ALWAYS logs after the console.log('Finalizing Transaction'). Every this.function() call returns a jquery deferred method in case you were wondering.

推荐答案

我在这里完全重新制作了jsFiddle: http://jsfiddle.net/JohnMunsch/nxPn3/

I completely remade my jsFiddle here: http://jsfiddle.net/JohnMunsch/nxPn3/

大约10次:)

.always()将在第一个.when()中完成的相同的Deferred对象(或Promise)返回给所有其他.always()函数.因此,他们迅速连任.看起来大概是这样.

.always() was returning the same Deferred object (or Promise) that had completed in the first .when() to all of the other .always() functions. So they went off in rapid fire succession. It looked roughly like this.

Step 1: log.
Step 2: kick off ajax call, log, kick off ajax call, log, kick off ajax call, log, etc. etc.

直到我在每个"ajax调用"函数中都放置了计时器以使它们延迟一段时间后,我才真正看到它.然后,我一直遇到这样的问题:如何在不嵌套上一步的.done()函数的每个下一步的情况下执行此操作?

I didn't really see it until I put timers in each of my "ajax call" functions to make them delay a while. Then I kept having the problem of, how do I do this without nesting each next step in the previous step's .done() function?

我对此的解决方案可能不是最优雅的,但它确实有效.我在前面创建了一套完整的Deferred对象,并使用它们来防止每个后续步骤都开始,直到上一步被标记为已解决或被拒绝为止.

My solution to that one may not be the most graceful, but it worked. I created a complete set of Deferred objects up front and I use them to prevent each successive step from starting until the previous step is either marked resolved or rejected.

下面是摘录:

console.log('Paying Charges due before transaction');
var step1 = applyCredit("parseFloat($(this.currentChargesTarget).html())");
var step2 = new $.Deferred();
var step3 = new $.Deferred();

$.when(step1).done(function(){
    console.log('Applying renewals');
    $.when(applyRenewals()).done(function () {
        step2.resolve();
    });
}).fail(function () { step2.reject() });

$.when(step2).done(function(){
    console.log('Paying renewal charges');
    $.when(applyCredit("some subcalc")).done(function () {
        step3.resolve();
    });
}).fail(function () { step3.reject() });

现在,当您运行jsFiddle时,每个函数调用都会沿着完美的一行进行.第二个要等到第一个完成后才能开始,而第三个要等到第二个完成后才能开始...

Now when you run the jsFiddle, each function call marches along in a perfect row. The second doesn't start until the first has finished and the third can't begin until the second is done...

这篇关于jQuery延迟链接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆