Javascript JS 测试是否为 10 的倍数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7129183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JS test if multiple of 10
提问by Adam Strudwick
In a JS function using setIntervall, I want to perform a jquery animation every 10 loops (in the other 9 loops, other animations are being displayed).
在使用 setIntervall 的 JS 函数中,我想每 10 个循环执行一个 jquery 动画(在其他 9 个循环中,正在显示其他动画)。
I am using the variable i in my function and it increments +1 each loop. Is there a very easy way to check in javascript if i is a multiple of 10 (in order to perform my jquery animation)?
我在我的函数中使用变量 i 并且它在每个循环中增加 +1。如果我是 10 的倍数(为了执行我的 jquery 动画),是否有一种非常简单的方法来检查 javascript?
In PHP I would simply do if(($i % 10) == 0)
... but I didn't find it in JS.
在 PHP 中我会简单地做if(($i % 10) == 0)
...但我没有在 JS 中找到它。
回答by Platinum Azure
Did you try it? I found a few sites that claim that the same operator %
will work in JavaScript.
你试了吗?我发现一些网站声称相同的运算符%
可以在 JavaScript 中工作。
回答by John Green
The modulus operator in JS works just fine.
JS 中的模运算符工作得很好。
for (var ii=0; ii < 100; ii++)
{
if (ii%10 == 0) console.log(ii);
}
回答by Russ Clarke
It still works in JS:
它仍然适用于 JS:
i = x % 10;
Here's a list of other operators:
以下是其他运营商的列表: