AngularJS $interval Service
Last Updated :
23 Jan, 2023
The $interval service in AngularJS is a function that allows you to execute a function repeatedly at a specified interval. It is similar to the setInterval function in JavaScript, but it is designed to work seamlessly with the AngularJS framework.
To use the $interval service, it is first needed to inject it into the AngularJS component as a dependency. For example, it can be injected into a controller like this:
angular.module('myModule', [])
.controller('MyController', function($scope, $interval) {
// Use the $interval service here
});
Syntax:
angular.module('myModule', [])
.controller('MyController', function($scope, $interval) {
var interval = $interval(function() {
//code
}, delay, count);
});
Parameters: The $interval service in AngularJS takes the following arguments:
- fn (function): The function to be executed at the specified interval.
- delay (number): The interval in milliseconds at which the function should be executed.
- count (number, optional): The number of times the function should be executed. If this argument is not specified, the function will be executed indefinitely until it is canceled.
Return type: A Promise will be returned while notifying for each iteration & will be resolved once each iteration of the interval got completed.
Approach 1: Here, we have an AngularJS application with a single controller, MyController. We have injected the $interval service into the controller as a dependency, and we have defined a countdown variable that is set to 10. We have also defined a finished flag that is initially set to false.
We are using the $interval service to execute a function every 1000 milliseconds (1 second). The function decreases the value of the countdown by 1 each time it is called, and it cancels the interval when the countdown reaches 0. When the countdown reaches 0, the finished flag is set to true. The value of the countdown is displayed on the page using an expression in the first p element, and the message “The countdown is finished!” is displayed when the `finished.
Example 1: This example describes the basic implementation of the $interval service in AngularJS, where we have defined a countdown variable & set it to 10.
HTML
<!DOCTYPE html>
< html ng-app = "myApp" >
< head >
< script src =
</ script >
< style >
h1 {
color: green;
}
</ style >
</ head >
< body ng-controller = "MyController" >
< center >
< h1 > GeeksforGeeks</ h1 >
< h3 >
AngularJS $interval service
</ h3 >
< p ng-if = "!finished" >Countdown: {{countdown}}</ p >
< p ng-if = "finished" >The countdown is finished!</ p >
</ center >
< script >
angular.module('myApp', [])
.controller('MyController', function ($scope, $interval) {
$scope.countdown = 10;
$scope.finished = false;
var interval = $interval(function () {
$scope.countdown--;
if ($scope.countdown === 0) {
$interval.cancel(interval);
$scope.finished = true;
}
}, 1000);
});
</ script >
</ body >
</ html >
|
Output:
Approach 2: Here, we have an AngularJS application with a single controller, MyController. We have injected the $interval service into the controller as a dependency, and we have defined the following variables and functions:
- elapsedTime: A variable that tracks the elapsed time in seconds.
- running: A flag that indicates whether the stopwatch is running.
- start: A function that starts the stopwatch.
- stop: A function that stops the stopwatch.
- reset: A function that stops the stopwatch and resets the elapsed time to 0.
Example 2: This is another basic example that describes the implementation of AngularJS $interval service, where we have created a elapsed Timer with start, reset & stop buttons.
HTML
<!DOCTYPE html>
< html ng-app = "myApp" >
< head >
< style >
.green {
color: green;
}
.black {
color: black;
}
h1 {
color: green
}
button {
color: white;
background-color: black;
height: 30px;
width: 100px;
padding: 3px;
margin: 5px;
border-radius: 5px;
}
</ style >
< script src =
</ script >
< script >
angular.module('myApp', [])
.controller('MyController', function ($scope, $interval) {
$scope.elapsedTime = 0;
$scope.running = false;
var interval;
$scope.start = function () {
if (!$scope.running) {
interval = $interval(function () {
$scope.elapsedTime++;
}, 1000);
$scope.running = true;
}
}
$scope.stop = function () {
if ($scope.running) {
$interval.cancel(interval);
$scope.running = false;
}
}
$scope.reset = function () {
$scope.stop();
$scope.elapsedTime = 0;
}
});
</ script >
</ head >
< body ng-controller = "MyController" >
< center >
< h1 > GeeksforGeeks</ h1 >
< h3 >AngularJS $interval service</ h3 >
< p ng-class =
"{'green': elapsedTime % 2 == 0, 'black': elapsedTime % 2 == 1}" >
Elapsed Time: {{elapsedTime}} seconds
</ p >
< button ng-click = "start()"
ng-disabled = "running" >
Start
</ button >
< button ng-click = "stop()"
ng-disabled = "!running" >
Stop
</ button >
< button ng-click = "reset()" >
Reset
</ button >
</ center >
</ body >
</html
|
Output:
Reference: https://docs.angularjs.org/api/ng/service/$interval
Similar Reads
AngularJS $controller Service
AngularJS applications depend on a controller to control the flow of data through an AngularJS application. AngularJS architecture uses the MVC model i.e the Model View Controller. The model is responsible for maintaining the application data, the View for displaying data or some part of data to the
5 min read
AngularJS $locale service
The AngularJS $locale service is a built-in service that provides localization information for the current browser environment. It is designed to help developers create applications that can be easily adapted to different locales and languages. The $locale service exposes a number of properties that
4 min read
Angular.js $log Service
The $log service in Angular.js is simply used for logging purposes on the browser console. It is used for the debugging and troubleshooting of the error in the code. It has various implementations like a log, warn, info, error, and debugging, and all the names suggest. It is used for logging, warnin
5 min read
AngularJS $animateCSS Service
The $animateCss service in AngularJS allows developers to easily animate the addition or removal of CSS classes on an element. This feature can be useful for creating visually appealing transitions and adding polish to the user interface. To use the $animateCss service, the ngAnimate module must be
4 min read
AngularJS $parse Service
The $parse service in AngularJS is a function that takes an expression string and returns a function that can be used to parse and evaluate the expression. The expression string can contain variables, operators, and function calls. To use the $parse service, you first need to inject it into your Ang
3 min read
AngularJS Services
The Services is a function or an object that avails or limit to the application in AngularJS, ie., it is used to create variables/data that can be shared and can be used outside the component in which it is defined. Service facilitates built-in service or can make our own service. The Service can on
4 min read
AngularJS $document Service
In AngularJS, a service is a function or object that is available for dependency injection (DI) in an AngularJS app. Services are typically used to encapsulate and reuse business logic and other app functionality that is not directly related to the presentation of data in the app. The $document serv
3 min read
AngularJS $location Service
The $location in AngularJS basically uses a window.location service. The $location is used to read or change the URL in the browser and it is used to reflect that URL on our page. Any change made in the URL is stored in the $location service in AngularJS. There are various methods in the $location s
4 min read
AngularJS $rootElement service
The AngularJS $rootElement service is a global service that provides a reference to the root element of the AngularJS application. This can be useful in a variety of scenarios, such as modifying the root element's class name or attributes, or traversing the root element's descendants. Syntax: The sy
3 min read
AngularJS $exceptionHandler Service
In AngularJS, a service is a function or object that is available for dependency injection (DI) in an AngularJS app. Services are typically used to encapsulate and reuse business logic and other app functionality that is not directly related to the presentation of data in the app. The $exceptionHand
4 min read