javascript - “ 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)
解决方案
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, ....
推荐阅读
- python - 从 Python 中的字母数字字符串列表执行数字降序然后升序排序
- cmake - 为什么 xerces 将 XMLCh 更改为 char16_t,我可以将其更改回 wchar_t 吗?
- excel - 将特定列提取到多个 .CSV 中,代码错误
- firebase - 如何从 Firestore 中检索这些数据?
- powerbi - 如何避免 pbix 选择“第一个”值?
- powershell - 在开始作业脚本块中使用 \\?\UNC\ (长 unc 路径)时,get-itemPropertyValue 不绑定路径
- wordpress - 禁用 Wocommerce 订阅续订订单
- python - 如何在 Django 1.11 中创建迁移删除表?
- javascript - 为 nodeJS 使用外部 conf 文件
- python - 带有层缓存的 Docker 构建时机密