当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Trigger after every 3rd time

问题描述:

This question already has an answer here:

  • Javascript run a function inside a loop every x iterations

    5 answers

网友答案:

You could...

  • Increment i by 3, not 1
  • Use Modulo to check

So

for (i = 0; i < 5000; i=i+3) {
...
}

Or

for (i = 0; i < 5000; i++) {
    // When i == 3, == 6, == 9 etc.
    if (i % 3 == 0) {
      trigger();
    }
}
网友答案:

Use the modulo operator:

if (i % 3 === 0) ...

condition is true when i is 0, 3, 6, ...

分享给朋友:
您可能感兴趣的文章:
随机阅读: