jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了javascript – 在jquery中延迟链接的条件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
假设我链接了$.像这样延迟.

$.each(data,function(k,v) {
    promise.then(function() {
        return $.post(...);
    }).then(function(data) {
        if(data)... // here is the conditions
        return $.post(...);
    }).then(function(data) {
        if(data)... // here is another condition
        return $.post(...);
    })
});

promise.done(function() {
    console.log("All Done!");
});

我做得对吗?如果条件返回false,如何阻止下一个链执行,以及如何执行此操作:

if(data){
   console.log('success');
}

这些代码可以介于那些.之间吗?

解决方法

乔伊,无论你是否做得对,取决于你想要达到的细节.

如果您尝试使用终端.done()构建一个长.then()链,其中每个.then()的’done’处理程序:

>调用异步进程,或
>透明地将数据传递到链中的下一个.then()

那么,代码应该是以下形式:

var promise = ...;//An expression that returns a resolved or resolvable promise,to get the chain started.

$.each(data,v) {
    promise = promise.then(function() {//The `.then()` chain is built by assignment 
        if(data...) { return $.post(...); }
        else { return data; }//Transparent pass-through of `data`
    }).then(function(data) {
        if(data...) { return $.post(...); }
        else { return data; }//Transparent pass-through of `data`
    });
});

promise.done(function() {
    console.log("All Done!");
}).fail(function(jqXHR) {
    console.log("Incomplete - an ajax call Failed");
});

但是,如果您尝试执行相同的操作,但每个.then()的’done’处理程序也是如此:

>调用异步进程,或
>中断.then()链

那么,v) { promise = promise.then(function(data) { if(data...) { return $.post(...); } else { return $.Deferred().reject(data).promise(); }//Force the chain to bE interrupted }).then(function(data) { if(data...) { return $.post(...); } else { return $.Deferred().reject(data).promise(); }//Force the chain to bE interrupted }); }); promise.done(function() { console.log("All Done!"); }).fail(function(obj) {//Note: `obj` may be a data object or an jqXHR object depending on what caused rejection. console.log("Incomplete - an ajax call Failed or returned data determined that the then() chain should bE interrupted"); });

大佬总结

以上是大佬教程为你收集整理的javascript – 在jquery中延迟链接的条件全部内容,希望文章能够帮你解决javascript – 在jquery中延迟链接的条件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。