0

I want to change the speed of steinterval() while running I tried something like this:

inteval = 1;
setInterval(function(){
    }, interval);
interval = 2;

Then I tried this which doesn't use intervals, but seems to work fine:

// Time
inteval = 1;
function refresh() {
    setTimeout(function() {refresh()},interval);
    }
refresh();
interval = 2;

I would rather have used setinterval() but this approach works fine. However in another similar question there is a longer approach: Changing the interval of SetInterval while it's running

If my snippet good in terms of efficiency? Is there a more efficient approach?

Community
  • 1
  • 1
lisovaccaro
  • 32,502
  • 98
  • 258
  • 410

1 Answers1

0

How about something like this:

var interval = 1000;
var times = 0;

function runInterval() {
    times ++;
    if(times == 5) {
        interval = 3000;
        clearInterval(s);
        s = setInterval(runInterval, interval);
    }
    console.log(times);
}
var s = setInterval(runInterval, interval);​

If you want to use setInterval, this is a way to do it. I hope it can help you