Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.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
Javascript 如何调试/解决添加重复项的递归ajax?_Javascript_Jquery_Ajax_Recursion - Fatal编程技术网

Javascript 如何调试/解决添加重复项的递归ajax?

Javascript 如何调试/解决添加重复项的递归ajax?,javascript,jquery,ajax,recursion,Javascript,Jquery,Ajax,Recursion,我目前正在开发一个纯JS/Jquery应用程序,遇到了一个问题 场景 我们有一个接受ID数组的类。并通过对Parse.com数据库的Ajax调用自行构建。构建后,它会将一些数据(Id、名称等)附加到列表中,弹出列表的新Id并调用其本身,直到数组中不再有Id为止。结果是一个包含所有构建对象列表的DIV 这里是伪代码中的类 function classSomething(ListOfIds,...){ var aListId= ListOfIds.pop(); $.ajax({ url: '

我目前正在开发一个纯JS/Jquery应用程序,遇到了一个问题

场景 我们有一个接受ID数组的类。并通过对Parse.com数据库的Ajax调用自行构建。构建后,它会将一些数据(Id、名称等)附加到
列表中,弹出列表的新Id并调用其本身,直到数组中不再有Id为止。结果是一个包含所有构建对象列表的DIV

这里是伪代码中的类

function classSomething(ListOfIds,...){
var aListId= ListOfIds.pop();
$.ajax({
    url: '<secret URL to Parse Database>' + aListId,
    dataType: 'json',
    async : true,
    success: function(response) {

        this.id = response.id;
        this.name = response.name;
        this.address = response.Address;

        ...

        var innerHTML = '<li><div class="aclassObject">'+ this.id + ... + '</div></li>';

        $('#TheList').append(innerHTML)

        if(ListOfIds.length >0){
                new classSomething(ListOfIds,...)
        }

    }
}
函数classSomething(ListOfIds,…){
var aListId=ListOfIds.pop();
$.ajax({
url:''+aListId,
数据类型:“json”,
async:true,
成功:功能(响应){
this.id=response.id;
this.name=response.name;
this.address=response.address;
...
var innerHTML='
  • '+this.id+…++'
  • '; $('#TheList').append(innerHTML) 如果(ListOfIds.length>0){ 新的classSomething(ListOfIds,…) } } }
    问题
    问题是列表有时有重复的元素。不幸的是,当我调试JS时,它在100%的时间内都能完美地工作,但当正常执行时,它会有“随机”元素重复项。

    尝试类似的操作,删除所有重复项,以便每个递归调用都有一个uniq ID数组。您可以抓取我放置的筛选器表达式,看看这是否解决了问题,但我猜是所有调用的异步性质造成了问题

    但是,如果一个ajax调用的返回时间比另一个要长一点怎么办?因为它都是异步的,你可以发送一个没有ID的调用,但是得到一个上一个调用的返回,这将返回它(不知道你刚刚删除了它)。下面是一些伪代码,也许可以激发一些想法

    function request(id) {    
    
        // return the AJAX promise
        return $.ajax({
            url: '<secret URL to Parse Database>' + id,
            dataType: 'json',
            async : true
        });
    }
    
    function sendTheRequest(ids,....) {
        // you probably don't need this filter if you go the promise route
        const id = ids.filter(function(item, pos) {
                     return ids.indexOf(item) == pos;
                   })
             return request(id).then(function(response){
                // deal with your returned data
                // conditional. if still 'ids', do recursive, call again
                  if(ids.length) sendTheRequest(ids,...)
             });
    }
    
    function putAllIds(ids){
        return sendTheRequest(ids);
    }
    
    putAllIds().done(function(res) {
        console.log(res);
    }
    
    功能请求(id){
    //回报AJAX承诺
    返回$.ajax({
    url:''+id,
    数据类型:“json”,
    异步:true
    });
    }
    函数sendTheRequest(ID…){
    //如果您选择承诺路线,则可能不需要此过滤器
    const id=id.filter(功能(项目、位置){
    返回ID.indexOf(项目)==位置;
    })
    返回请求(id)。然后(函数(响应){
    //处理返回的数据
    //条件。如果仍然是“ID”,则执行递归,然后再次调用
    如果(id.length)发送请求(id,…)
    });
    }
    函数putAllIds(id){
    返回发送请求(ID);
    }
    putAllIds().done(函数(res){
    控制台日志(res);
    }
    
    尝试类似的方法,删除所有重复项,以便每个递归调用都有一个uniq ID数组。您只需抓取我放置的筛选器表达式,看看这是否解决了问题,但我猜是所有调用的异步性质造成了问题

    但是,如果一个ajax调用的返回时间比另一个要长一点怎么办?因为它都是异步的,你可以发送一个没有ID的调用,但是得到一个上一个调用的返回,这将返回它(不知道你刚刚删除了它)。下面是一些伪代码,也许可以激发一些想法

    function request(id) {    
    
        // return the AJAX promise
        return $.ajax({
            url: '<secret URL to Parse Database>' + id,
            dataType: 'json',
            async : true
        });
    }
    
    function sendTheRequest(ids,....) {
        // you probably don't need this filter if you go the promise route
        const id = ids.filter(function(item, pos) {
                     return ids.indexOf(item) == pos;
                   })
             return request(id).then(function(response){
                // deal with your returned data
                // conditional. if still 'ids', do recursive, call again
                  if(ids.length) sendTheRequest(ids,...)
             });
    }
    
    function putAllIds(ids){
        return sendTheRequest(ids);
    }
    
    putAllIds().done(function(res) {
        console.log(res);
    }
    
    功能请求(id){
    //回报AJAX承诺
    返回$.ajax({
    url:''+id,
    数据类型:“json”,
    异步:true
    });
    }
    函数sendTheRequest(ID…){
    //如果您选择承诺路线,则可能不需要此过滤器
    const id=id.filter(功能(项目、位置){
    返回ID.indexOf(项目)==位置;
    })
    返回请求(id)。然后(函数(响应){
    //处理返回的数据
    //条件。如果仍然是“ID”,则执行递归,然后再次调用
    如果(id.length)发送请求(id,…)
    });
    }
    函数putAllIds(id){
    返回发送请求(ID);
    }
    putAllIds().done(函数(res){
    控制台日志(res);
    }