1

Is there a possible way for me to set a timer for the client side to periodically make a request to the server automatically?

For example

Polling(){

this.http.makeRequestEvery1min(){

subscribe(data => {

)
}
//request should be every sent every 1 minute 

}
XamarinDevil
  • 873
  • 5
  • 17
  • 38

3 Answers3

1
Rx.Observable.interval(60*1000).
  switchMap(x=> http.getSomething())
  .subscribe(x=>console.log(x))
Julia Passynkova
  • 17,256
  • 6
  • 33
  • 32
0

You can use Javascript's setInterval function inside ngOnInit.

setInterval(function(){ // Do Something }, 3000)

Sumit Dhameja
  • 141
  • 2
  • 9
0

You have to use the interval function.

.controller
( 'sampleController',['$scope','$interval', function ($scope, $interval) {


       function Polling(){

          //Write your http request here.
      }


var interval = 1000; //in milliseconds

var intervalPromise = $interval(polling, 1000); 


//To Kill the interval function on page closure or route change

$scope.$on('$destroy', function () {
        if (angular.isDefined(intervalPromise)) {
            $interval.cancel(intervalPromise);
        }
    });

}]);
Kalyan
  • 1,200
  • 1
  • 11
  • 23
  • 1
    have look at this stackoverflow post for example. http://stackoverflow.com/questions/35316583/angular2-http-at-an-interval – Kalyan Apr 19 '17 at 01:54