等待。每()来完成,考虑到。每个()的Ajax调用 [英] Wait For .each() to Finish, Given .each() Has AJAX Calls

查看:84
本文介绍了等待。每()来完成,考虑到。每个()的Ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/9043968/continue-execution-only-after-each-completes\">Continue执行。每个()完成中后才


这问题实际上是从这个讨论的延续。我们如何可以等待每()鉴于有是完成它的执行 $。获得()的回调函数内?

工作的例子可以发现, 这里

  / *的JavaScript / jQuery的。 * /
&LT;脚本&GT;
函数prepareLayer($ N){
    $获得('./ a.html'功能(数据){
        / * a.html包含:其中; A HREF =JavaScript的:无效(0);&gt;点击ME&LT; / A&GT;! * /
        $ n.html(数据);
    });
}功能后preparation(){
    $('元素')。点击(函数(){
        警报('哎哟......你刚刚点击我!');
    });
}$(函数(){
    $('元')。每个(函数(){
        prepareLayer($(本));
    });    后preparation();
});
&LT; / SCRIPT&GT;&LT;! - HTML - &GT;
&LT; D​​IV CLASS =元素&GT;&LT; / DIV&GT;
&LT; D​​IV CLASS =元素&GT;&LT; / DIV&GT;


解决方案

@Alnitak给你最在这一问题解决的:<一href=\"http://stackoverflow.com/questions/9043968/continue-execution-only-after-each-completes\">Continue执行。每个()完成中后才

  VAR DEF = [];
$('元')。每个(函数(){
    //有prepareLayer返回_promise_返回
    def.push(prepareLayer());
}//用当地叫后preparation每一次
//承诺已得到解决
$ .when.apply($,DEF).done(后preparation);

缺少的部分看起来像

 函数prepareLayer($ N){
    VAR DFD = $递延()。
    $获得('./ a.html'功能(数据){
        / * a.html包含:其中; A HREF =JavaScript的:无效(0);&gt;点击ME&LT; / A&GT;! * /
        $ n.html(数据);
        dfd.resolve();
    });
    返回dfd.promise();
}

或者使用jQuery> = 1.8,@ jfriend00

 函数prepareLayer($ N){
    返回$获得('./ a.html'),然后(功能(数据){
        $ n.html(数据);
    });
}

Possible Duplicate:
Continue Execution Only After .each() Completes

This question is actually a continuation from this discussion. How can we wait each() to finish its execution given that there is $.get() inside its callback function?

Working example can be found here.

/* JavaScript / jQuery. */
<script>        
function prepareLayer($n) {
    $.get('./a.html', function(data) {
        /* a.html contains: <a href="javascript:void(0);">Click me!</a> */
        $n.html(data);
    });
}

function postPreparation() {
    $('.element a').click(function() {
        alert('Ouch... you just clicked me!');
    });
}

$(function() {
    $('.element').each(function() {
        prepareLayer($(this));
    });

    postPreparation();
});
</script>

<!-- HTML -->
<div class="element"></div>
<div class="element"></div>

解决方案

@Alnitak gave you most of the solution in this question : Continue Execution Only After .each() Completes

var def = [];
$('.element').each(function() {
    // have prepareLayer return a _promise_ to return
    def.push(prepareLayer());
}

// use "when" to call "postPreparation" once every
// promise has been resolved
$.when.apply($, def).done(postPreparation);

The missing piece would look like

function prepareLayer($n) {
    var dfd=$.Deferred();
    $.get('./a.html', function(data) {
        /* a.html contains: <a href="javascript:void(0);">Click me!</a> */
        $n.html(data);
        dfd.resolve();
    });
    return dfd.promise();
}

Or with jQuery>=1.8, courtesy of @jfriend00

function prepareLayer($n) {
    return $.get('./a.html').then(function(data) {
        $n.html(data);
    });
}

这篇关于等待。每()来完成,考虑到。每个()的Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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