This is simple functionality. You can use this script when you want get updates in ajax call periodically like update cricket or football score.
With $interval
- Define one function with ajax call with $http
- Call above function with $interval and time in milliseconds
Script
function myCtrl($scope, $http, $timeout, $interval) { $scope.count = 0; $scope.ajaxPeriodicall = function() { $http.get('/angular/sample.csv'). success(function(data, status, headers, config) { // this callback will be called asynchronously // when the response is available $scope.count = $scope.count + 1; }); }; $scope.start = function() { $scope.myCall = $interval($scope.ajaxPeriodicall, 1000); }; $scope.stop = function() { $interval.cancel($scope.myCall); }; }
With $timeout
- Define one function with ajax call with $http
- In response function call that function again with $timeout
Script
$scope.count = 0; $scope.ajaxPeriodicall = function() { $http.get('/angular/sample.csv'). success(function(data, status, headers, config) { // this callback will be called asynchronously // when the response is available // call ajaxPeriodicall with timeout $scope.count = $scope.count + 1; $timeout($scope.ajaxPeriodicall, 1000); }); };
Well, it's very well this example.
ReplyDeleteThank you!
https://best-exam-questions.mystrikingly.com/blog/pfmp-portfolio-management-professional
ReplyDelete