简体   繁体   English

循环jQuery $ .get,如何知道循环何时完成

[英]Loop jquery $.get, how to know when loop is finished

I'm trying to do a loop where I load some content from another page using jquery $.get. 我正在尝试做一个循环,使用jquery $ .get从另一个页面加载一些内容。

Here is what I am doing: 这是我在做什么:

for(var i=0; i<users.length; i++) {
    var myURL = "http://url.com/user.php?user=";

    myURL = myURL + users[i];

    $.get(myURL, function(data){
        $('table').append( data )
    });

}

That part all works fine, it updates my table with the data returned. 那部分一切正常,它使用返回的数据更新我的表。 My problem is that I want to show a loading icon, or even just text that says "loading" until it finishes loading all the data from the other page. 我的问题是,我想显示一个加载图标,甚至只是显示“正在加载”的文本,直到它完成了从另一页加载所有数据为止。

I'll spare you from all the things I have tried that don't work, because I really suck at JS, and it will be embarrassing. 我将尽我所能尝试的所有不起作用的方法,从我那里抽出来,因为我真的很喜欢JS,这将令人尴尬。

Any suggestions would be appreciated. 任何建议,将不胜感激。

var doneCounter = 0;
for(var i=0; i<users.length; i++) {
    var myURL = "http://url.com/user.php?user=";

    myURL = myURL + users[i];

    $.get(myURL, function(data){
        $('table').append( data );
        doneCounter++;
        if(doneCounter == users.length)  hideBusy(); 
    });

}

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

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