14

Do angular models work with setIntervaL?

Here's an example.. if I have

<p>{{number}}</p>
<button ng-click="testFunc()">TEST</button>

For some reason it updates WITHOUT setInterval, but like this it doesn't:

scope.testFunc = function(){
    setInterval(function(){
        scope.number--;
    },1000);
};

Without setInterval the number will update every single click, but with setInverval it won't update continuously. If I click it every 5 seconds it'll jump a bunch of numbers up (so it's running in the background but the view isn't updating).

5 Answers 5

21

What's happening is that you're using setInterval which is outside of "Angular's world" so it's not updating the DOM. You have two options, one is a lot better than the other.

Use $interval. It's just like setInterval except it's part of the Angular world, so those changes will update your view (be sure to pass $interval into your directive).

scope.testFunc = function(){
    $interval(function(){
        scope.number--;
    },1000);
};

The second option is to call apply() on your current code to kick off a digest cycle, don't do that though since there is a better option.

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

2 Comments

Tried using $interval but it returned Error: $interval is not defined.... BUT $digest() worked
Yeah. Like I mentioned you have to pass $interval into your controller. angular.module('myCtrl', function($scope, $interval)...
6

See the documentation for the interval in angular js Interval with Angular JS

You don't to use the $digest to work with interval. See the code below:

HTML

<div ng-app ng-controller="ApplicationController">   
    <pre>Number: {{ number }}</pre>
    <button ng-click="testTimer()">Test Timer</button>
</div>

JS

function ApplicationController($scope,$interval) {

    $scope.number = 99999;

    $scope.testTimer = function() {
        $interval(function() {
            $scope.number--;
        }, 100);
    };  
}

See this example in action here

Comments

1

Angular comes with a nice wrapper for setInterval. Use it nearly the same way.

https://docs.angularjs.org/api/ng/service/$interval

1 Comment

is it injected into your controller? myApp.controller(function($scope, $interval) {...})
0

$interval works definitely you can learn more about it at: https://docs.angularjs.org/api/ng/service/$interval

as a further note, instead of using {{ number }} try using <p ng-model="number"></p> to show your value instead.

Comments

-1

Tried using $interval but it returned Error: $interval is not defined.... BUT $digest() worked:

scope.testFunc = function(){
    setInterval(function(){
        scope.number--;
        scope.$digest();
    },1000);
};

2 Comments

No! Don't do this for the reasons I mentioned in my answer.
The reason why it didn't work is because you haven't injected $interval

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.