0

my html code is :

<select ng-model='refresh_rate' ng-change="onRefreshRateChange()">
    <option value='10000'>10</option>
    <option value='30000' selected>30</option>  
    <option value='60000'>60</option>
    <option value='180000'>180</option>
    <option value='300000'>300</option>
</select>

and in my angular controller is :

var intervaL = $interval(function(){
    //[here]
    //after run every times i want update delay to $scope.refresh_rate
}, $scope.refresh_rate);

see this line in top code : //here i want update delay time to $scope.refresh_rate

now i want a code that put it Instead of this line

5
  • There is too much code there that has nothing to do with the question. When posting questions, try to reduce the code to a minimal that still reproduces and highlights your issue. This will help others help you. Commented Jan 27, 2015 at 21:03
  • @new-dev sorry for my bad ask... i want in angularjs $interval function after every times run update delay of $interval to $scope.refresh_rate Commented Jan 27, 2015 at 21:10
  • You can always (and should) edit your question to clean it. That's the whole point of these comments Commented Jan 27, 2015 at 21:11
  • @NewDev done...i edited my question.. how i can do it ? Commented Jan 27, 2015 at 21:14
  • Easiest would be to create a $timeout loop, then you can set the new $timeout to $scope.refresh every time. Commented Jan 27, 2015 at 21:19

2 Answers 2

6

To change the $interval rate you need to stop one $interval and start another.

Without going into particulars of whether the last $interval call should fire or cancel immediately - I'll let you handle that on your own - you can do the following:

var p = $interval(doSomething, $scope.refresh_rate);

$scope.$watch("refresh", function(){
  $interval.cancel(p);
  p = $interval(doSomething, $scope.refresh_rate);
});

function doSomething(){
  //...
}

plunker

EDIT:

You can also certainly do this in onRefreshRateChange instead of $scope.$watch. The difference is whether you care to change $scope.refresh_rate outside of your <select> - this is where you would use $watch vs. only in response to a change in <select>

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

8 Comments

doSomething function most be in controller function?
It needs to be accessible to the controller function, but yes - it's better if placed in the controller function.
The issue with this is that it will cancel the interval function before firing it off, so you may be missing calls here and there.
@ZackArgyle, yes, that's why I said "Without going into particulars" and "I'll let you handle that on your own", since this was not specified as a requirement in the original question.
what?! :) This is a Q&A site - ask whatever you need here, and either I or others would help you
|
2

Use a $timeout loop.

function loop(fn) {
    $timeout(function() {
        if (!$scope.stopBefore) {
            fn();
            $scope.stopAfter || loop(fn);
        }
    , $scope.refresh);
}
loop(doSomething);

5 Comments

That's a good approach - so long as there is no requirement to cancel immediately and start another timer.
Added some flags that could be used for control.
But not to be able to start the next timer right away. My bigger point is - the OP's requirements were not accurate
@ZackArgyle i most instead of $scope.refresh use $scope.refresh_rate ??
@ZackArgyle Work but if find my answer from New Dev Answer.. any way tank you :X

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.