首页 > 解决方案 > “ i = (i + 1) % word.length ” 背后的逻辑是什么

问题描述

我是编码新手。谁能解释一下(“i =(i + 1)% word.length”)

var text = document.querySelector('#text-wrap');

var word = text.getElementsByTagName('span');

var i = 0;

   function run(){

        word[i].style.display = 'none';

        i = (i + 1) % word.length

        word[i].style.display = 'initial';
  }

setInterval(run,800)  

标签: javascripthtmldom

解决方案


if (i < word.length - 1):

  i = i + 1; // increment

else if (i == word.length - 1):

  i = 0; // go back to 0

例如,如果word.length为 5,初始i为 0,则

i的输出序列是 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, ....


推荐阅读