AngularJS Timer Services
Posted By : Deepak Mittal | 31-Dec-2014
AngularJS has two timer services:-
1. $timeout:-
The $timeout
service can be used to call another JS function after a given time delay. The $timeout
service only schedules a single call to the function. For repeated calling of a function.To use $timeout service you must inject this in your controller and once the $timeout
service is injected into your controller, you can use it to schedule function calls. Here is an example that used the $timeout
service to schedule a function call 3 seconds later:
var app = angular.module("app", []);
app.controller("MyController", function($scope, $timeout){
$timeout(callingTimeout, 3000); });
function callingTimeout() {
console.log("Timeout started");
}
2. $interval:-
The $interval
service is similar in function to the $timeout
service, except it schedules a function for repeated execution with a time interval in between.And to use the $interval
service you must have it injected into a controller function. Once the $interval
service is injected into your controller function, you can use it to schedule repeated function calls. Here is an example that used the $interval
service to schedule a function call every 3 seconds:
var app = angular.module("app", []); app.controller("MyController", function($scope, $interval){ $interval(callingInterval, 3000); }); function callingInterval() { console.log("Interval started"); }
which you can use to call functions in your application.
THANKS
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Deepak Mittal
Deepak is a bright web Developer. Deepak has expertise in Java.