0

I want to use a dynamic delay for interval:

var tabDelay = 1500;
var timer = $interval(function() {
    if (some if) {

            tabDelay = 5000;

        } else {

            tabDelay = 1000;

        }
    }
},tabDelay);

But seems that this is not the right way. How could I use dynamic values for $interval delay in angularJs?

TheNone
  • 5,684
  • 13
  • 57
  • 98

2 Answers2

3

Consider using $timeout as I don't believe you can change an interval's duration

Sean3z
  • 3,745
  • 6
  • 26
  • 33
  • But I have to use it in angular material tab like slider : http://stackoverflow.com/questions/34919979/angular-material-sliding-tabs/34921355#34921355 – TheNone Jan 21 '16 at 14:02
  • @TheNone Interval is trivial to implement using timeout, but gives you some flexibility like changing timeout time. – dfsq Jan 21 '16 at 14:06
  • @TheNone You could always clear the interval and reset it using an example like this http://stackoverflow.com/a/7445863/1128459 (in your case, you'd use `$interval.cancel(timer);` – Sean3z Jan 21 '16 at 14:09
0

I would personally recommend recurring $timeout.

The main advantage of this approach is that, if you have an async method inside, you can wait for that async to happen and then trigger another timeout. This is particularly useful when pooling for different data sets.

var delay = 500, clearTimeout, stop = false;
function doLoad (stop) {
  if (x) {
    delay = 1000;
  } else {
    delay = 500;
  }

  if (myCondition) {
    stop = true;
  }

  // do stuff
  if (stop) {
    if (clearTimeout) {
      $timeout.cancel(clearTimeout);
    }
  } else {
    clearTimeout = $timeout(doLoad, delay);
  }
}


doLoad(delay);

Would this be an option? What are the constraints on Angular Material?

Dragos Rusu
  • 1,508
  • 14
  • 14