2017-12-31 315 views
0

Angular 4プロジェクトには、いつでも他の関数(A()B()と呼ぶことができます)によって呼び出すことができる関数(reload())があります。私はA()またはB()の最後の呼び出しからX時間(すなわち、msecs)が経過するまでreload()の実行をデバウンスしたいと思います。私は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. 
+0

あなたが探しているのは 'throttleTime'ですか? – martin

+0

@msanford例を挙げてください。 –

答えて

2

あなたはdebounceTimeSubjectを使用することができます。だから両方の機能を持っている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(); 
    }); 
} 

ここではこれをデモするstack blitzです。

関連する問題