Adam Strudwick
Adam Strudwick

Reputation: 13129

JS test if multiple of 10

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.

Upvotes: 15

Views: 30096

Answers (3)

Russ Clarke
Russ Clarke

Reputation: 17909

It still works in JS:

i = x % 10;

Here's a list of other operators:

http://www.w3schools.com/js/js_operators.asp

Upvotes: 9

John Green
John Green

Reputation: 13435

The modulus operator in JS works just fine.

for (var ii=0; ii < 100; ii++)
{
    if (ii%10 == 0) console.log(ii);
}

Upvotes: 14

Platinum Azure
Platinum Azure

Reputation: 46193

Did you try it? I found a few sites that claim that the same operator % will work in JavaScript.

Upvotes: 22

Related Questions