简体   繁体   English

使用 Observable 的 Angular 8 轮询

[英]Angular 8 Polling Using Observables

Hey i am upgrading my Project from angular 5 to angular 8. This is the code i was using for polling.嘿,我正在将我的项目从 angular 5 升级到 angular 8。这是我用于轮询的代码。

 Observable.interval(this.intervalTime).timeout(600000)
                .takeWhile(() => this.alive)
                .subscribe(i => {
                    this.timeOutValue++;
                    //fun();
                });

fun() is setting alive as true or false. fun() 将活动设置为真或假。 But now in angular 8 interval is not defined.但是现在在 angular 8 区间没有定义。 Will someone please help me for workaround.有人请帮助我解决方法。

Use the interval function from 'rxjs' and perform your operations in a pipe.使用 'rxjs' 中的interval函数并在管道中执行您的操作。

import { interval } from 'rxjs';
import { timeout, takeWhile } from 'rxjs/operators';

class MyClass {
  private intervalTime: number;
  private alive: Subject<any> = new Subject<any>();
  private timeOutValue = 0;

  myPollingMethod() {
    interval(this.intervalTime).pipe(
      timeout(600000),
      takeWhile(() => this.alive)
    ).subscribe(i => {
      this.timeOutValue++;
      //fun();
    });
  }
}

Use a throttle time for continuous polling or debounce使用节流时间进行连续轮询或去抖动

Rx.Observable.interval(1000).throttleTime(2000).subscribe(x => console.log())
const interval$ = interval(1000);
const debouncedInterval = interval$.pipe(debounce(val => timer(val * 200)));
const subscribe = debouncedInterval.subscribe(val =>
console.log(`Example Two: ${val}`)
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM