Skip to content Skip to sidebar Skip to footer

How To Make A New Request With $http.get Every 3 Seconds And Get Data Outside Function

I want to get some data from this page http://orbit5.ds.cs.umu.se:8888/vrio/debug/blk. the data on this page updates every time you refresh the page. So I would like to have these

Solution 1:

Use $q of angular, you need to inject $q as dependency,

functiongetData(){

  var deffered = $q.defer();
  var data;
  $http.get(path).then(function(response){
    data = response.data
    $q.resolve(data);
  })
  return deffered.promise;
}

var getDataCall = getData();

getDataCall.then(function(data){
console.log(data);
})

if you want to repeat this call use, $interval(fn, delay, [count], [invokeApply], [Pass]);

see doc https://docs.angularjs.org/api/ng/service/$interval

Try this for repeating, (i haven't tried),

functiongetData(){   
  var deffered = $q.defer();
  var data;
  $http.get(path).then(function(response){
    data = response.data
    $q.resolve(data);
  })
  return deffered.promise;
}


$interval(function(){
  getData().then(function(data){
  console.log(data);
  });
},3000);

Solution 2:

You just need to run the code in an interval:

setInterval( function(){
    $http.get(path).then(function(response){
        console.log(response.data);
        // Update your page here
    });
}, 3000 );

Solution 3:

You want to use Angular's $interval module to do this.

Essentially, you have a function called getData which uses the $http module. Then, in the resolution of that promise, you set the $scope.chartData with the new data.

You then call that function on an interval:

$interval($scope.getData, 3000); // every 3 seconds

You don't need $q if you do this in your controller, but you should probably take it out of the controller and put it in a service and then use the $q module.

Here is a CodePen on HTTP polling in Angular with a full Angular app as the solution that works with your provided endpoint.

Update: If you wanted to call it from an external function outside of your controller, you can do that by creating a factory that uses $q and injecting that into your controller. The same example but with a factory: CodePen on HTTP polling in Angular (using a factory)

You'll also note the $scope.getBytesWritten method in the example with the factory/service. You need to execute a function in order to get part of the data—simply trying to access a property of the chart data won't update that single value. Please refer to the CodePen on HTTP polling in Angular (using a factory).

Solution 4:

Hi,

In AngularJS , there is a service called $timeout , that does exactly what you're looking for in one loop.

Check the whole official documentation here : $timeout service documentation

Solution 5:

Using the $interval service with the $http service

The $http service returns a promise that you can use for chaining. Use the $interval service to repeditively create an new promise and chain from that promise to put the updated data on scope.

Return that promise and chain from it.

angular.module("myApp").controller("myVm",
  function($scope,$http,$interval) {
    var count = 0;

    functiongetHttpPromise(url) {
        var promise = $http.post(url);
        //return for chainingreturn promise;
    };

    functionupdateScope(promise) {
        //chain from promise
        promise.then (function (response) {
            count++;
            $scope.currentCount = "Current response "+count;
        });        
    };

    updateScope(getHttpPromise("/echo/json"));

    var intervalPromise =  $interval( function(){
        updateScope(getHttpPromise("/echo/json"));
    }, 1000 );

    $scope.onStopUpdate = function () {
        $interval.cancel(intervalPromise);
    });
});

The above example has a function getHttpPromise that returns a promise for chaining. The updateScope function chains from the httpPromise to update the scope. The example then uses the $interval service to update the scope every 1000 milliseconds.

The $interval service also returns a promise that can be used for chaining and for cancellation.

The DEMO on JSFiddle.

Post a Comment for "How To Make A New Request With $http.get Every 3 Seconds And Get Data Outside Function"