jQuery每个超时 [英] jQuery each with timeout

查看:115
本文介绍了jQuery每个超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下功能,除了一件事情以外,它运行正常。

 (function biograf(){
var placeholder = $(' (顶级图片框)';
$('ul#biomenu> li')。each(function(){
var obj = $(this);
var pageLink = obj。 find('a:first')。attr('href');
$ .get(pageLink,function(data){
placeholder.append($(data).find('#top- );
placeholder.append($(data).find('#top-imageholder')。siblings('ul'));
});
});
})();

我希望这个函数能够在我的列表项目放置在#biomenu的顺序中附加占位符。

它可以在90%的时间内完成,但偶尔会以不同的顺序追加。



任何建议?

解决方案

好吧,它可以使用下面的代码:

 (函数biograf(){
var placeholder = $('#top-imageholder');
var item = $('ul#biomenu> ; li');
var index = 0;

(function loop(){
setTimeout(function(){
var pageLink = item.eq(index ).find('a:first')。attr('href');
if(pageLink!= undefined){
$ .get(pageLink,function(data){
placeholder .append($(data).find('#top-imageholder')。html());
placeholder.append($(data).find('#top-imageholder')。siblings('ul '));
});
index ++;
loop();
}
});
})();
})();

我不确定这是否是最优化的方式,或者是否可以调整代码。但它的作品! :)



感谢所有回覆!


I have the following function which runs OK except for one thing. There is no consequence in what it appends first.

(function biograf(){
  var placeholder = $('#top-imageholder');
  $('ul#biomenu > li').each(function(){
    var obj = $(this);
    var pageLink = obj.find('a:first').attr('href');
    $.get(pageLink, function(data) {
      placeholder.append( $(data).find('#top-imageholder').html() );
      placeholder.append( $(data).find('#top-imageholder').siblings('ul') );
    });
  });
})();

I would like the function to append the placeholder in the order my list items are placed in the #biomenu.

It does this 90% of the time, but once in a while it appends in different orders.

Any suggestions?

解决方案

Well got it to work with the following code:

(function biograf(){
  var placeholder = $('#top-imageholder');
  var item = $('ul#biomenu > li');
  var index = 0;

  (function loop(){
    setTimeout(function(){
      var pageLink = item.eq(index).find('a:first').attr('href');
      if( pageLink != undefined ){
        $.get(pageLink, function(data) {
          placeholder.append( $(data).find('#top-imageholder').html() );
          placeholder.append( $(data).find('#top-imageholder').siblings('ul') );
        });     
        index++;
        loop();
      }
    });
  })();
})();

I'm not sure if this is the most optimal way, or if the code could be tweeked. But it works! :)

Thanks for all the replies though!

这篇关于jQuery每个超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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