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).
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)?
In PHP I would simply do if(($i % 10) == 0)
... but I didn't find it in JS.
When you multiply a number by 10 you will get the number attached with zero as the result. eg: 10 × 2 = 20. Multiples of 10 are even numbers that end with 0. If a number is ending with 0 then it is always divisible by 10.
To check if one number is a multiple of another number, use the modulo operator % . The operator returns the remainder when one number is divided by another number. The remainder will only be zero if the first number is a multiple of the second.
Did you try it? I found a few sites that claim that the same operator %
will work in JavaScript.
The modulus operator in JS works just fine.
for (var ii=0; ii < 100; ii++)
{
if (ii%10 == 0) console.log(ii);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With