2017-04-19 19 views
1

クライアント側のタイマーを定期的に設定してサーバーに自動的にリクエストする方法はありますか?あなたはngOnInit内のJavascriptののsetInterval関数を使用することができHTTPリクエストのタイマー設定

例えば

Polling(){ 

this.http.makeRequestEvery1min(){ 

subscribe(data => { 

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

} 

答えて

1
Rx.Observable.interval(60*1000). 
    switchMap(x=> http.getSomething()) 
    .subscribe(x=>console.log(x)) 
+0

これは最も簡単な方法です。 – Thibs

0

のsetInterval(関数(){// が何かをする}、3000)

0

あなたはインターバル機能を使用する必要があります。

.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); 
     } 
    }); 

}]); 
+1

例えば、このstackoverflowの投稿を見てください。 http://stackoverflow.com/questions/35316583/angular2-http-at-an-interval – Kalyan

関連する問題