简体   繁体   English

setInterval 是如何工作的?

[英]setInterval how does it work?

i want that at first Cell E is blinking red blue red blue... and then Cell H is blinking red blue red......我希望首先单元格 E 闪烁红蓝红蓝……然后单元格 H 闪烁红蓝红……

But only Cell H is blinking.但只有 Cell H 在闪烁。

What is to change?要改变什么?

it seems that it only run for i = selectCell=2似乎它只针对 i = selectCell=2 运行

<script>

var blinkColors=new    Array('red','blue','red','blue','red','blue','red','blue');

var selectCell = 2;
 for (var i = 0; i <=selectCell -1; i++)

 {
        var blinkColor=0;

        var myBlink=setInterval(function(){doBlink(j);},300);

          function doBlink(x)
            {
             var jj=x;
             var blinkCell=document.getElementById('blinker'+jj);
             blinkCell.style.backgroundColor=blinkColors[blinkColor];
             blinkColor++;

             if(blinkColor==blinkColors.length)
                 {
                  stopBlink();
                 }

            }

          function stopBlink()
                {
                 clearInterval(myBlink);
                }
    }

</script>
</head>

Here is an updated example.这是一个更新的示例。

 var blinkColors = new Array('red', 'blue', 'red', 'blue', 'red', 'blue', 'red', 'blue'); var selectCell = 2; var blinkColor = 0; var iterator = 0; var myBlink = setInterval(function() { doBlink(); }, 300); function doBlink() { var blinkCell = document.getElementById('blinker' + iterator); blinkCell.style.backgroundColor = blinkColors[blinkColor]; blinkColor++; if (blinkColor == blinkColors.length) { blinkColor = 0; blinkCell.style.backgroundColor = "transparent"; iterator++; if (iterator == selectCell) clearInterval(myBlink); else doBlink(iterator); } }
 <div id="blinker0">Cell 1</div> <div id="blinker1">Cell 2</div>

I am creating a dynamic table using javascript Array.我正在使用 javascript 数组创建一个动态表。 In the array, zw are the indices of the cells which should blink.在数组中, zw是应该闪烁的单元格的索引。 I have tried a lot but only the first cell is blinking.我尝试了很多,但只有第一个单元格在闪烁。 Here is my code这是我的代码

var blinkColors = new Array('red', 'white','red', 'white','red', 'white' );
     var zw= new Array(1,4,5,9);
     var blinkColor = 0;
     var iterator = 0;

      var myBlink = setInterval(function() {
        doBlink();
      }, 300);

    function doBlink() {

       var blinkCell=document.getElementsByTagName('td');
       blinkCell[zw[iterator]].style.backgroundColor=blinkColors[blinkColor];

      if (blinkColor == blinkColors.length)

      {
        blinkColor = 0;
        blinkCell.style.backgroundColor = "transparent";
        iterator++;

        if (iterator == zw.lenght)
          clearInterval(myBlink);
        else
          doBlink(zw[iterator]);
       }
    }
 }

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

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