Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.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 当不在代码块之外工作时_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 当不在代码块之外工作时

Javascript 当不在代码块之外工作时,javascript,jquery,ajax,Javascript,Jquery,Ajax,我目前正在玩烧杯浏览器API,我需要向其他用户的网站发出几个(至少20个)ajax请求,以便获得他们发布的内容 我有以下阵列: var posts = []; // Where the data of the ajax is stored var ajax_requests = []; // Where the ajax requests are stored var following = []; // Where the urls for all requests are 以下逻辑在所有链

我目前正在玩烧杯浏览器API,我需要向其他用户的网站发出几个(至少20个)ajax请求,以便获得他们发布的内容

我有以下阵列:

var posts = []; // Where the data of the ajax is stored
var ajax_requests = []; // Where the ajax requests are stored
var following = []; // Where the urls for all requests are
以下逻辑在所有链接中循环

// Get posts from all the people you follow
for(var i = 0; i < following.length; i++) {

    // Get the person's url and folders
    let person = new DatArchive(following[i]);

    // Read the directory containing all the posts
    person.readdir("/posts").then(files => {

        // For each post
        for(var j = 0; j < files.length; j++) {
            // Request the JSON object representing that post and add it to the array
            ajax_requests.push(get_posts(person.url + "/posts/" + files[j]));
        }
    });
}
我试图理解的是,当没有等待ajax完成时,
$。我认为这就是它应该做的,相反,它返回空数组。这就是一个原因,另一个原因是,如果我将它移动到
person.readdir(“/posts”)
块中,它会起作用

我不能把它放在那里,因为我想进入的逻辑需要对posts数组进行排序,只显示其中的一部分,我认为这不应该是一次页面加载运行不止一次的事情。

您可以使用
$.map()
$.when.apply()
模式两次等待来自内部循环的所有结果

following=[1,2,3];
函数get_posts(url){
返回新的延迟美元(dfd=>
setTimeout(dfd.resolve,Math.floor(Math.random()*1000),url));
}  
$.when.apply($
,$.map(以下,(跟随者)=>{
let person=新的延迟美元(dfd=>
setTimeout(dfd.resolve,Math.floor(Math.random()*1000)
,数组(3)。填充(跟随器))
);
返回人
。然后(文件=>
$.when.apply($
,$.map(文件,(文件)=>
获取帖子(“/posts/”+文件)))
)
}))
.then(函数(…数据){
控制台日志(数据);
});

您可以在调用
$.when.apply()之前检查
ajax\u请求
数组
.length
是否等于
后面的.length
乘以
文件.length

var following=[1,2,3];
var ajax_requests=[];
函数get_posts(url){
返回延迟的美元(dfd=>
setTimeout(dfd.resolve,Math.floor(Math.random()*1000),url));
}
对于(变量i=0;i
setTimeout(dfd.resolve,Math.floor(Math.random()*1000)
,数组(3)。填充(在[i]之后)
);
//读取包含所有帖子的目录
然后(文件=>{
//每个职位
对于(var j=0;j


if语句似乎没有运行,下面和上面的所有语句都运行,但if跳过。@Alexexan“似乎没有运行”是什么意思?stacksnippets的模式与问题代码的模式相同。在
.then()
处生成的数组链接到
$。在stacksnippets()处生成的数组包含jQuery promise对象的解析值。您可能应该在代码中包含错误处理。如果任何jQuery promise对象抛出一个错误
。那么()
链接到
$。当()
无法到达时。由于不同的person对象不包含相同数量的文件,因此它不起作用,我的问题是有缺陷的。@Alexexan您可以在其他答案中使用该方法,它不检查数组的
.length
function get_posts(url) {
    return $.ajax({
        type: "GET",
        dataType: "json",
        url: url,
        success: function(data) {
            console.log("Adding to posts");
            posts.push(data);
        }
    });
}    



$.when.apply($, ajax_requests).then(function() {
    console.log(posts.length);
});