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?

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

But I have to use it in angular material tab like slider : stackoverflow.com/questions/34919979/…
@TheNone Interval is trivial to implement using timeout, but gives you some flexibility like changing timeout time.
@TheNone You could always clear the interval and reset it using an example like this stackoverflow.com/a/7445863/1128459 (in your case, you'd use $interval.cancel(timer);
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?

3 Comments

There is no any constraint in material. But in my example the above code not worked.
the above code is "pseudo code" - you'd have to implement for your own. what's the output?
I know :) I have used recursive function for this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.