2017-12-31 45 views
0

각도 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

당신은 SubjectdebounceTime로 사용할 수 있습니다. 그래서 두 가지 기능을 모두 가지고 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입니다.

관련 문제