1. Home
  2. Languages
  3. AngularJS
  4. Mastering AngularJS: Scheduling Repeated Updates with $interval

Mastering AngularJS: Scheduling Repeated Updates with $interval


Keeping your AngularJS app’s data fresh and responsive often means running periodic updates. Whether you’re refreshing a live feed, updating user stats, or syncing data in the background, AngularJS’s $interval service is a powerful tool for scheduling repeated tasks. In this guide, we’ll walk through how to implement scheduled updates using $interval effectively — complete with best practices to keep your app performant and bug-free.


What is $interval?

$interval is AngularJS’s wrapper around the native JavaScript setInterval function. However, it’s much better integrated with Angular’s digest cycle, ensuring that your UI updates seamlessly when data changes.


Why Use $interval for Scheduled Updates?

Using $interval allows you to:

  • Automate data refreshes without user intervention.
  • Keep your UI in sync with server-side changes.
  • Perform background tasks on a set schedule.
  • Leverage AngularJS’s built-in lifecycle management.


Step-by-Step Guide to Implement $interval Updates

1. Inject the $interval Service

Start by injecting $interval into your controller or service where you want to schedule updates:

javascript
app.controller(‘MyController’, function($scope, $interval, DataService) {
// Your code here
});

2. Create the Update Function

Define the function that will fetch or compute data to refresh your view. For example:

javascript
$scope.updateData = function() {
DataService.getLatest()
.then(function(response) {
$scope.data = response.data;
})
.catch(function(error) {
console.error(‘Failed to update data:’, error);
});
};

3. Schedule the Repeated Updates

Use $interval to run the update function at your desired interval (e.g., every 5 seconds):

javascript
var intervalPromise = $interval($scope.updateData, 5000);

This sets up the automatic periodic fetching.

4. Clean Up to Prevent Memory Leaks

AngularJS doesn’t automatically stop $interval when your scope is destroyed. To prevent memory leaks or unwanted background tasks, always cancel your interval when the controller or component is destroyed:

javascript
$scope.$on(‘$destroy’, function() {
if (angular.isDefined(intervalPromise)) {
$interval.cancel(intervalPromise);
intervalPromise = undefined;
}
});


Full Example

Putting it all together:

javascript
app.controller(‘MyController’, function($scope, $interval, DataService) {
$scope.data = [];

$scope.updateData = function() {
DataService.getLatest()
.then(function(response) {
$scope.data = response.data;
})
.catch(function(error) {
console.error('Failed to update data:', error);
});
};
// Initial fetch
$scope.updateData();
// Schedule updates every 5 seconds
var intervalPromise = $interval($scope.updateData, 5000);
// Clean up when scope is destroyed
$scope.$on('$destroy', function() {
if (angular.isDefined(intervalPromise)) {
$interval.cancel(intervalPromise);
intervalPromise = undefined;
}
});

});


Pro Tips for Working with $interval

  • Use $interval over setInterval to ensure Angular fully integrates the timing into its digest cycle.
  • Always cancel intervals during scope destruction.
  • Avoid extremely short intervals as frequent updates can degrade performance.
  • Consider $timeout for single delayed executions or if you want to control recursive invocation more granularly.
  • Use promises returned by $interval to chain tasks if needed.


Wrapping Up

Scheduling updates with $interval in AngularJS is straightforward but requires attention to cleanup and performance implications. By following these best practices, you ensure your app stays responsive, efficient, and maintainable.

Keep your users engaged with real-time data that refreshes automatically — all thanks to the elegance of AngularJS’s $interval.

Updated on July 5, 2025
Was this article helpful?

Related Articles

Leave a Comment