AngularJS $timeout Service

Angular.js $timeout service is the same as windows.SetTimeout function in javascript. $timeout function can be used to set time delay to execute our functions or methods when required. For instance we need to redirect to another page after 10 seconds. We can use $timeout functionality here to do that. The syntax for using $timeout service.

var app = angular.module('timeoutApp', []);
app.controller('timeoutCtrl', function ($scope, $timeout) {
$scope.msg="Hi Guest"
$timeout(function () {
$scope.msg = "Welcome to Tutlane.com";
}, 3000);
});

In the above example we are changing messages after 3 seconds. To use $timeout service as an argument in the controller in angularjs we need to pass $timeout as a parameter. 

Another example :


<!DOCTYPE html>


<html>

<head>

<title>

AngularJs $timeout Service with Example

</title>

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<script type="text/javascript">

var app = angular.module('timeoutApp', []);

app.controller('timeoutCtrl', function ($scope, $timeout) {

$scope.msg="Hi Guest"

$timeout(function () {


$scope.msg = "Welcome to Tutlane.com";

}, 3000);

});

</script>

</head>

<body>

<div ng-app="timeoutApp" ng-controller="timeoutCtrl">

<p>AngularJS $timeout Function Example</p>

<b>{{msg}}</b>

</div>

</body>

</html>