Stephanie D.
Stephanie D.

Reputation: 119

Skip a setInterval every X loop?

I have a function that gets triggered every 10 seconds with a setInterval(); loop, and I'd like it to skip a call every 60 seconds. So the function would be executed at 10s, 20s, 30s, 40s and 50s but wouldn't be called at 60s, then gets called at 70s and so on.

Is there any straightforward way to do this, or do I have to tinker with incrementing a variable?

Thanks!

Upvotes: 0

Views: 945

Answers (1)

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

Do like this, use the simple modulo math and a counter.

var count = 0;
var interval = setInterval(function(){ 
      count += 1;
      if(count % 6 !== 0) {
          // your code
      } 
}, 10000);

Upvotes: 3

Related Questions