简体   繁体   English

jQuery each和计时循环

[英]jQuery each and timing loops

I am trying to build a series of nested timing loops. 我正在尝试构建一系列嵌套的计时循环。 The inner loop iterates through 6 items with the same CSS class and swaps out an image for 4 seconds. 内部循环使用相同的CSS类遍历6个项目,并将图像换出4秒钟。 The outer loop causes the inner loop to repeat continuously. 外循环使内循环连续重复。 So, image1 swaps, image2 swaps... image6 swaps, image1 swaps, image2 swaps... There are two images in each div, one with class 'hot' and one with class 'cold'. 因此,image1交换,image2交换... image6交换,image1交换,image2交换...每个div中有两张图像,其中一张的类别为“热”,而一张的类别为“冷”。 At the start, the 'hot' images are hidden. 开始时,“热”图像被隐藏。

The following code swaps all the images at once, after 24 seconds, and then doesn't seem to do anything else. 以下代码在24秒后立即交换所有图像,然后似乎什么也没做。

        var innertime = 4000; //highlight effect time in ms
        var outertime = innertime * 6;
        setInterval(function() {
            $.each($('.eachsponsor'), function(){
                $(this).find('img.cold').css("display","none");
                $(this).find('img.hot').css("display", "block");
                setTimeout(function(){
                    $(this).find('img.hot').css("display","none");
                    $(this).find('img.cold').css("display", "block");
                }, innertime);
            });
        }, outertime);

If anybody has any pointers on why this doesn't work, I'd sure appreciate it. 如果有人对为什么它不起作用有任何指示,我肯定会感激的。

Your problem is: this inside the setTimeout is the global. 您的问题是: setTimeout中的this是全局的。 Try binding the context using .bind 尝试使用.bind绑定上下文

setTimeout(function(){
           $(this).find('img.hot').css("display","none");
           $(this).find('img.cold').css("display", "block");
      }.bind(this),innertime);

But I think your code still does not do what you want, I think you need this: 但是我认为您的代码仍然无法满足您的要求,我认为您需要这样做:

var innertime = 4000;
var eachsponsor = $('.eachsponsor');
$.each(eachsponsor, function(){
       $(this).find('img.cold').hide();
       $(this).find('img.hot').show();
});

var currentIndex = 0;
setInterval(function(){
      var currentDiv = eachsponsor.eq(currentIndex);
      currentDiv.find('img.hot').toggle();
      currentDiv.find('img.cold').toggle();

      currentIndex = (currentIndex+1) % eachsponsor.length;
 }, innertime);

Updated: 更新:

var innertime = 4000;
    var eachsponsor = $('.eachsponsor');
    $.each(eachsponsor, function(){
           $(this).find('img.cold').hide();
           $(this).find('img.hot').show();
    });

    var currentIndex = 0;

    function slideNext(){
          var currentDiv = eachsponsor.eq(currentIndex);

          currentDiv.find('img.cold').hide();
          currentDiv.find('img.hot').show();

          setTimeout(function(){
              currentDiv.find('img.hot').toggle();
              currentDiv.find('img.cold').toggle();
              currentIndex = (currentIndex+1) % eachsponsor.length;
              slideNext();
          },innertime);
    }

    slideNext();

this variable, inside setTimeout function, does not have reference of outside this. setTimeout函数内部的此变量没有此外部的引用。 do something like following 做类似的事情

setInterval(function() {
            $.each($('.eachsponsor'), function(){
                var that = this;
                $(this).find('img.cold').css("display","none");
                $(this).find('img.hot').css("display", "block");
                setTimeout(function(){
                    $(that).find('img.hot').css("display","none");
                    $(that).find('img.cold').css("display", "block");
                }, innertime);
            });
        }, outertime);

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

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