递归Ajax延迟对象 [英] Recursive Ajax deferred object

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

问题描述

当递归ajax函数结束递归时,我需要在DOM中粘贴一些数据。

I need to paste some data in the DOM when a recursive ajax function ends its recursion.

我遇到的问题是$ .when()。done()将在递归的第一步触发,但我需要它在最后一步触发递归结束时,递归结束。

The problem I have is that "$.when().done()" will trigger in the first step of rescursion but I need that it triggers on the last step of recursion, when recursion ends.

我真的不知道如何实现它,任何帮助都会受到赞赏!

I really have no idea how to achive it, any help will be kindly apreciated!

function recursiveFunction(data) {

    // do something with data
    // and remove elements processed from data

    var param = {
            "data" : data
    };
    return $.ajax({
            data:  param,
            url:   'script.php',
            type:  'post',
            success:  function(response) {
                recursiveFunction(response);
            },
    });
}

$.when(recursiveFunction(data)).done(function(response){
    paintData();
});


推荐答案

您可以使用 deffered。 promise 用于处理递归执行的对象。我已修改您的示例以提供一些数据并停止条件。这里的示例 - > jsfiddle

You can use deffered.promise object to handle recursive execution. I've modified your example to provide some data and stop condition. Example here -> jsfiddle

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

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