JQuery - 在'for'语句中循环一个.load() [英] JQuery - Looping a .load() inside a 'for' statement

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

问题描述

我不确定这是否真的可行,因为load()是一个异步方法,但我需要一些方法来基本上加载几个小小的页面,一次一个,获取一些数据包含在他们JavaScript,然后通过Ajax发送,所以我可以把它放在我制作的数据库。

基本上我从我的页面得到这个,在那里我所有的链接

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

link =http://www.digimon-heroes.com+ $(digiList).eq (ⅰ).find( '地图')的儿童()ATTR( 'href' 属性)。;

到目前为止好。

现在,我将不得不加载每个链接(只有整个页面的特定部分,而不是整个东西)到一个div我有我的页面周围的某处,以便我可以通过JQuery获取一些数据:

  var contentURI = link +'div.row:nth-​​child(2)'; 
$ b $('#single')。load('grabber.php?url ='+ contentURI,function(){
/////////////我在这里做了一堆JQuery的东西,并将东西保存到一个对象
/////////////然后我调用一个ajax请求
$ .ajax( {
url:'insertDigi.php',
type:'POST',
data:{digimon:JSON.stringify(digimon)},
dataType:'json',
成功:函数(msg){
console.log(msg);
}
////////这个调用了一个处理所有事情的脚本,插入我的数据库
}); // END ajax
}); //结束加载回调函数
} // END'for'语句。
alert('插入!');

当然,正如所料,加载时间过长,对于 语句只是继续,而不是真的关心让负载完成它的业务,因为负载是异步的。在我甚至有机会加载第一页之前调用alert('Inserted!'); 。这反过来又意味着我只能将这些东西加载到我的div中,然后才能处理它的信息并将其发送给我的脚本。所以我的问题是:是否有创造性的方式来做到这一点,我可以通过多个链接迭代,加载它们,与他们做生意,完成它?如果没有,是否有一个同步替代负载,可以产生大致相同的效果?我知道这可能会阻止我的网页完全,但我会很好,因为该网页不需要任何输入。



希望我解释一切都有了必要的细节,希望你们可以帮我解决这个问题。谢谢!

解决方案

您可能需要一个递归函数,等待一个迭代,然后再进行下一次迭代等。 p>

 (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){

//做data
$ b $ .ajax({
url:'insertDigi.php',
类型:'POST',
data: {
digimon:digimon
},
dataType:'json'
})。done(function(msg){
console.log(msg);
if(i< digiList.length){
recursive(++ i); //做下一个...当这是一个完成
}
});
});
})(0);


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.

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!'); 

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 - 在'for'语句中循环一个.load()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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