Executing $digest() After the Scheduled Function Call
If the function you schedule for execution makes changes to variables in the $scope
object, or make changes to any other variable which your application is watching, your application needs to execute $scope.$digest()
after the scheduled function call finishes. Why that is necessary is explained in my tutorial about $watch(), $digest() and $apply().
By default AngularJS already calls $digest()
after the scheduled function call finishes, so you don't have to do that explicitly. You can, however, specify if AngularJS should not call $digest()
after the scheduled function call. If, for instance, your scheduled function call only updates an animation but does not change any $scope
variables, then it is a waste of CPU time to call $digest()
after the function finishes.
Both $timeout
and $interval
have a third, optional parameter which can specify if the $digest()
method is to be executed after the scheduled function finishes. Actually, the third parameter specifies if the call to the scheduled function should be done inside an $apply()
call. Here is an example of how to use this third parameter:
$interval( function(){ $scope.callAtInterval(); }, 3000, true); $interval( function(){ $scope.callAtInterval(); }, 3000, false);
These two $interval
examples both have a third parameter passed to the $interval
service. This parameter can be either true
or false
. A value of true
means that the scheduled function should be called inside an $apply()
call. A value of false
means that it should not be called inside an $apply()
call (meaning $digest()
will not get called after the scheduled function finishes).