डाउनलोड पी.डी.एफ. ई-बुक्स
AJS - Services $interval

$interval service; ये Object; Javascript के window.setInterval() Method जैसे होता है | ये window.setInteral() method का wrapper होता है |


Example for $interval Service in AngularJS

Source Code :
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</head>
<body>

<div ng-app="app" ng-controller="ctrl"> 
Current Time : {{clock}}
</div>

<script>
var myapp = angular.module('app', []);
myapp.controller('ctrl', function($scope, $interval) {
$scope.clock = new Date().toTimeString();
var currtime = function(){
	$scope.clock = new Date().toTimeString();
}
$interval(currtime, 1000);
});
</script>

</body>
</html>
Output :
Current Time : {{clock}}

Another Example for $interval Service in AngularJS

Example पर हर 3 seconds(3000 milliseconds) के बाद $scope.count property को 1 से increase किया जायेगा |

Source Code :
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</head>
<body>

<div ng-app="app" ng-controller="ctrl"> 
Count : {{count}}
</div>

<script>
var myapp = angular.module('app', []);
myapp.controller('ctrl', function($scope, $interval){
$scope.count = 0;
var counter = function (){
	$scope.count = $scope.count + 1;
}
$interval(counter, 3000);
});
</script>

</body>
</html>

Another Example for $interval Service in AngularJS

Example पर हर 1 second(1 milliseconds) के बाद $scope.count property को 1 से 10 तक increase किया जाएगा |

Source Code :
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</head>
<body>

<div ng-app="app" ng-controller="ctrl"> 
Count : {{count}}
</div>

<script>
var myapp = angular.module('app', []);
myapp.controller('ctrl', function($scope, $interval){
$scope.count = 0;
var counter = function (){
	$scope.count = $scope.count + 1;
}
$interval(counter, 1000, 10);
});
</script>

</body>
</html>

$interval.cancel() Method in AngularJS

Example पर जब 'Stop Time' button पर click किया जायेगा तब current time को stop किया जायेगा |

Source Code :
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</head>
<body>

<div ng-app="app" ng-controller="ctrl"> 
Current Time : {{clock}}<br />
<button ng-click="stop()">Stop Time</button>
</div>

<script>
var myapp = angular.module('app', []);
myapp.controller('ctrl', function($scope, $interval) {
$scope.clock = new Date().toTimeString();
var currtime = function(){
	$scope.clock = new Date().toTimeString();
}
var myVar = $interval(currtime, 1000);
$scope.stop = function () {
    $interval.cancel(myVar);
}

});
</script>

</body>
</html>