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

  1. Define one function with ajax call with $http
  2. 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

  1. Define one function with ajax call with $http
  2. 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);
      }); 
     
};

1 comment:

Blogroll

Popular Posts