jQuery-在'中循环.load()陈述 [英] JQuery - Looping a .load() inside a 'for' statement

查看:32
本文介绍了jQuery-在'中循环.load()陈述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这是否真的可能,因为load()是一种异步方法,但是我需要一种方法来基本上加载几页的页面,一次加载一次,通过来获取其中包含的一些数据JavaScript,然后通过Ajax发送,这样我就可以将其放到我制作的数据库中.

I'm not sure if this will actually be possible, since load() is an asynchronous method, but I need some way to basically Load several little bits of pages, one at a time, get some data included in them via JavaScript, and then send that over via Ajax so I can put it on a database I made.

基本上我是从我的页面上得到的,我将要遍历的所有链接都位于其中:

Basically I get this from my page, where all the links I'll be having to iterate through are located:

var digiList = $('.2u');
var link;
for(var i=0;i<digiList.length;i++){

    link = "http://www.digimon-heroes.com" + $(digiList).eq(i).find('map').children().attr('href');

到目前为止一切都很好.

So far so good.

现在,我将不得不将每个链接(仅整页的特定div,而不是整个页面)加载到页面周围某个地方的div中,以便可以通过JQuery获取一些数据:

Now, I'm going to have to load each link (only a specific div of the full page, not the whole thing) into a div I have somewhere around my page, so that I can get some data via JQuery:

 var contentURI= link + ' div.row:nth-child(2)';   

    $('#single').load('grabber.php?url='+ contentURI,function(){     
 ///////////// And I do a bunch of JQuery stuff here, and save stuff into an object 
 ///////////// Aaaand then I call up an ajax request.
     $.ajax({
      url: 'insertDigi.php',
      type: 'POST',
      data: {digimon: JSON.stringify(digimon)},
      dataType: 'json',
      success: function(msg){
        console.log(msg);
      }
 ////////This calls up a script that handles everything and makes an insert into my database.
    }); //END ajax
   }); //END load callback Function
  } //END 'for' Statement.
 alert('Inserted!'); 

自然,正如预期的那样,加载会花费很长时间,并且 for 语句的其余部分将一直进行下去,而不是真正在乎让负载完成其业务,因为负载是异步的.在我什至没有机会加载第一页之前就调用了 alert('Inserted!'); .反过来,这意味着我只能在甚至无法处理信息并将其发送到脚本之前将其加载到div中.

Naturally, as would be expected, the loading takes too long, and the rest of the for statement just keeps going through, not really caring about letting the load finish up it's business, since the load is asynchronous. The alert('Inserted!'); is called before I even get the chance to load the very first page. This, in turn, means that I only get to load the stuff into my div before I can even treat it's information and send it over to my script.

所以我的问题是:是否有某种创造性的方式来实现这一目的,使得我可以遍历多个链接,加载它们,与它们做生意并完成它?如果不是,是否有同步负载替代方案,可以产生大致相同的效果?我知道它可能会完全阻塞我的页面,但是我可以接受,因为该页面不需要我提供任何输入.

So my question is: Is there some creative way to do this in such a manner that I could iterate through multiple links, load them, do my business with them, and be done with it? And if not, is there a synchronous alternative to load, that could produce roughly the same effect? I know that it would probably block up my page completely, but I'd be fine with it, since the page does not require any input from me.

希望我对所有内容进行了必要的详细说明,希望大家能帮我解决这个问题.谢谢!

Hopefully I explained everything with the necessary detail, and hopefully you guys can help me out with this. Thanks!

推荐答案

您可能想要一个递归函数,该函数等待一个迭代,然后再进行下一个迭代,等等.

You probably want a recursive function, that waits for one iteration, before going to the next iteration etc.

(function recursive(i) {
    var digiList = $('.2u');
    var link = digiList.eq(i).find('map').children().attr('href') + ' div.row:nth-child(2)';
    $.ajax({
        url: 'grabber.php',
        data: {
            url: link
        }
    }).done(function(data) {

        // do stuff with "data"

        $.ajax({
            url: 'insertDigi.php',
            type: 'POST',
            data: {
                digimon: digimon
            },
            dataType: 'json'
        }).done(function(msg) {
            console.log(msg);
            if (i < digiList.length) {
                recursive(++i); // do the next one ... when this is one is done
            }
        });
    });
})(0);

这篇关于jQuery-在&amp;#39;中循环.load()陈述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆