Javascript如何用不稳定的迭代时间构建循环

Javascript how to build loop with unconstant time of iterations

本文关键字:时间 构建 循环 迭代 何用 不稳定 Javascript      更新时间:2023-09-26

需要JS jedi进行咨询。情境:我得到了一个USER_ID数组,需要调用社交网络功能在他们的墙上发帖。所以我需要启动wallpost函数,听它的答案,然后继续迭代我的循环。我写的代码可以把帖子留给数组中的最后一个人,第一个人,没有其他很多有用的函数:s。需要你的帮助jedis

    function showPost() {
            for(i in selFriends) {  //selFriends - massive with user_id 
                    alert(i+' '+selFriends[i]); //checkplace
                    VK.api('wall.post',{
                            owner_id:selFriends[i], //element
                            message:htmlres,  // content 
                            attachment:photoID // id of the mediacontent
                    },function(receive) {
                            showPost();
                    });
            return;
            }
    }

这段代码需要修复,因为现在它只需要迭代wall.post中的第一个元素。顺便说一句,VK.api方法几乎类似于Facebook的UI方法"提要"。非常感谢:)

我不是绝地武士,但您是否试图通过VK.api运行selFriends数组中的第一个索引,并在回调中调用下一个索引?然后对selFriends数组中的每个元素重复?

function postId(index) {
   if (index === selFriends.length) 
       return;
   VK.api('wall.post',{
       owner_id:selFriends[i], //element
       message:htmlres,  // content 
       attachment:photoID // id of the mediacontent
       },function(receive) {
           postId(index + 1);
   });
}

然后

function showPost() {
    postId(0);     
}