$timeout and $interval are two AngularJS timer services. We use these services to call functions and these are similar to javascript’s setTimeout and setInterval functions.
$timeout- This is used to call a function and schedules a single call to the function . It calls another javascript function after a certain time delay.
Using $timeout-
1.To use timeout, you need to inject it into your controller. For example-
var egapp = angular.module(egapp", []);
egapp.controller(EgController", function($scope, $timeout){
});
2.We can use this to make function calls,i.e. schedule a function call after a particular time delay. You can call a function by two methods. Below are the two methods-
a. For example:-
var egapp = angular.module(egapp", []);
egapp.controller(EgController", function($scope, $timeout){
$timeout(Display, 5000);
});
function Display() {
console.log("Timeout occurred");
}
b.
var egapp = angular.module(egapp", []);
egapp.controller(EgController", function($scope, $timeout){
$scope. Display = function() {
console.log("$scope.Display);// calling on the $scope object
}
$timeout( function(){
$scope.Display();
}, 5000);
});
Both the methods are set to call a function after 5 seconds(5000 milliseconds) of delay.
$interval-This is used to call a function and schedules repeated calls to the function . It calls another javascript function after a certain time delay.
Using $interval-
1.To use interval, you need to inject it into your controller. For example-
var egapp = angular.module(egapp", []);
egapp.controller(EgController", function($scope, $interval){
});
2.We can use this to make function calls,i.e. schedule repeated function calls after a particular time delay. You can call a function by two methods. Below are the two methods-
a. For example:-
var egapp = angular.module(egapp", []);
egapp.controller(EgController", function($scope, $interval){
$interval(Display, 5000);
});
function Display() {
console.log("Timeout occurred");
}
b.
var egapp = angular.module(egapp", []);
egapp.controller(EgController", function($scope, $interval){
$scope. Display = function() {
console.log("$scope.Display);// calling on the $scope object
}
$interval( function(){
$scope.Display();
}, 5000);
});
Both the methods are set to call Display function every 5 seconds(5000 milliseconds) of delay.
0 Comment(s)