简体   繁体   English

jQuery如何使循环等到load()完成

[英]jQuery how to make for loop wait until load() is completed

I need to load topics in "for" loop, but the loop is done before the topics start to load. 我需要在“ for”循环中加载主题,但是循环是在主题开始加载之前完成的。 Can someone help me? 有人能帮我吗? HTML code is fine, the only problem is jQuery and Javascript. HTML代码很好,唯一的问题是jQuery和Javascript。

var newTop = $("#invisForum").find('a:not(:has(span))'); //find every link which does not include span
var odkazy = new Array();
for (var i = 0; i < noveTop.length; i++) {
     odkazy[i] = $(noveTop[i]).attr('href'); //get the href attribute from every link
}

if (odkazy.length!=0){
   $("#unreadForum").css("visibility","visible"); //if there is any link, make table for links visible
}

for (var x = 0; x < odkazy.length; x++) {
    alert("The x in LOOP is "+ x);
    $("#spravneOdkazy").load(odkazy[x]+' td[style="white-space:normal;"]',function(){  
        topicy = $("#spravneOdkazy").find("td").find('img[alt="Uzavřeno"]');
        if (topicy.length>0){
             $("#unreadTable").find("tbody").append("<tr><th>"+$("#invisForum").find("span:nth-child("+x+")").text()+"</th></tr>");      
        }
        for (var y = 0; y < topicy.length; y++) {
             $(topicy[y]).parent().find("img").css("vertical-align","middle");        
             $("#unreadTable").find("tbody").append("<tr><td>"+$(topicy[y]).parent().html()+"</td></tr>");
        }
       alert("The X in LOAD is " +x);
    }); 
}

I want the output to be 我希望输出是

The X in LOOP is 0 LOOP中的X为0
The X in LOAD is 0 LOAD中的X为0
The X in LOOP is 1 LOOP中的X为1
The X in LOAD is 1 LOAD中的X为1
The X in LOOP is 2 LOOP中的X为2
The X in LOAD is 2 LOAD中的X为2
The X in LOOP is 3 LOOP中的X为3
The X in LOAD is 3 LOAD中的X为3

But the current output is 但是目前的输出是

The X in LOOP is 0 LOOP中的X为0
The X in LOOP is 1 LOOP中的X为1
The X in LOOP is 2 LOOP中的X为2
The X in LOOP is 3 LOOP中的X为3
The X in LOAD is 0 LOAD中的X为0
The X in LOAD is 1 LOAD中的X为1
The X in LOAD is 2 LOAD中的X为2
The X in LOAD is 3 LOAD中的X为3

Because the load() function takes a while to load and the loop completes before first load() finishes. 因为load()函数需要一段时间才能加载,并且循环在第一个load()完成之前完成。 Can I make the loop wait until every single load() is completed and then proceed? 我可以让循环等到每个load()完成之后再继续吗?

You could try creating a recursive function rather than an actual loop which has a call to itself in the .load() callback. 您可以尝试创建一个递归函数,而不是在.load()回调中调用自身的实际循环。

function loop(array,x){

    if( isNaN(x) ) x = 0;

    console.log("loop: "+x);

    $("element").load(array[x],function(){

        console.log("load: "+x);

        //Do your stuff here

        if( array.length > x+1 ) loop(array,x+1);

    });

}

var array = ["link1","link2","link3"];
loop(array);

This function will then operate much like a loop except that it will wait until the callback from the .load() before it runs again. 然后,此函数将像循环一样工作,不同之处在于它将等待直到.load()的回调才再次运行。

You are trying to force Javascript to work in a syncronous way, while using an jquerys asynchonous load-function. 您正在尝试使用JavaScript异步加载功能时强制Javascript以同步方式工作。

There is a good reason why you use asyncronous programming in a situation like this: because loading data across the internet takes a very long time compared to all the other steps in your program. 在这种情况下使用异步编程是有充分的理由的:因为与程序中的所有其他步骤相比,通过Internet加载数据需要很长时间。

I suggest you don't try to force this program into running in the syncrounous way you are used to. 我建议您不要强迫该程序以您惯用的方式运行。 Instead you should try to understand and use asyncronous programming where it is useful. 相反,您应该尝试在有用的地方理解和使用异步编程。 Any good book on Javascript will teach you the basics of this. 任何有关Javascript的好书都会教给您有关此方面的基础知识。 To delve deeper I recommend Burnham, T. 2012. ASYNC JavaScript: Build More Responsive Apps with Less Code. 为了更深入地研究,我推荐Burnham,T.2012。ASYNC JavaScript:用更少的代码构建更多响应式应用程序。 Pragmatic Bookshelf. 实用的书架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM