Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 设置HTTP请求的计时器_Angularjs_Angular - Fatal编程技术网

Angularjs 设置HTTP请求的计时器

Angularjs 设置HTTP请求的计时器,angularjs,angular,Angularjs,Angular,有没有一种方法可以让我设置一个计时器,让客户端定期自动向服务器发出请求 比如说 Polling(){ this.http.makeRequestEvery1min(){ subscribe(data => { ) } //request should be every sent every 1 minute } 您可以在ngOnInit中使用Javascript的setInterval函数 setInterval(函数(){ //做点什么},3000)你必须使用interval

有没有一种方法可以让我设置一个计时器,让客户端定期自动向服务器发出请求

比如说

Polling(){

this.http.makeRequestEvery1min(){

subscribe(data => {

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

}

您可以在ngOnInit中使用Javascript的setInterval函数

setInterval(函数(){
//做点什么},3000)

你必须使用interval函数

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

}]);

以stackoverflow帖子为例。这是最干净的方法。
.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);
        }
    });

}]);