【问题标题】:Angular & Observable debounceTimeAngular & Observable debounceTime
【发布时间】:2017-12-31 18:18:43
【问题描述】:

在一个 Angular 4 项目中,我有一个函数(我们称之为reload()),它可以随时被其他函数(我们称之为A()B())调用。我想解除reload() 的执行直到从A()B() 的最后一次调用过去X 时间(即毫秒)。我正在查看 Rx.Observable.debounceRx.Observable.debounceTime 函数,但我不明白它们是否真的可以帮助我。

一个例子:

time 0ms: A() gets executed and it calls reload()
time 200ms: B() calls executed and it calls reload()
Since X is set to 500ms, reload() should be called only once and after 500ms.

【问题讨论】:

  • throttleTime 是您要找的吗?
  • @msanford 你能举个例子吗?

标签: angular rxjs observable debounce


【解决方案1】:

您可以将SubjectdebounceTime 一起使用。所以让AB 这两个函数都向主题发送一个值。然后对主题流进行去抖动,这样只有在 x 时间过后才发出值。

// component.ts
subject$ = new Subject();
stream$;

constructor(){
    this.stream$ = this.subject$.debounceTime(1000);
}

A(){
    this.subject$.next('some value');
}
B(){
    this.subject$.next('some value');
}

ngOnInit(){
    this.stream$.subscribe(res => {
        this.reload();
    });
}

这是一个stack blitz 的演示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多