角度和可观察到的反跳时间

布鲁诺·布鲁扎诺

成多角4项目,我有一个函数(让我们叫它reload()),可以被其它函数调用(我们称它们A()B())在任何时间。我想对reload()A()的最后一次调用到X时间(即毫秒)的执行进行反跳操作B()我正在查看Rx.Observable.debounceandRx.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.

您可以将Subject搭配使用debounceTime因此,既要有这两个功能A又要B给主题发送一个值。然后,对主题流进行去抖动,以便在经过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();
    });
}

这是一个演示这堆栈闪电战

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章