Rxjs create timer in the subscribe handler?

时间:2018-06-04 17:35:06

标签: javascript rxjs

I have a subscription to an observable, and I'm trying to run a timer handler five seconds after the last emit value of the observable. The timer is then reset on the next emit

// If the service does not emit for 5 second, the timeout handler is called, 
// then the timer begins again on the next service emit
service.on('update')
            .subscribe((event) => {                
                this.updateCounter(event);
                // Clear previous timer
                this.timerSub && this.timerSub.unsubscribe()
                // Create new timer
                this.timerSub = timer(5000)
                    .subscribe( () => {
                        this.timerHandler();
                    });
            });

....
timerHandler() {
   // do something
} 

This works ok, but I'm wondering if there's a way of doing this that is more in line with Rxjs patterns?

2 个答案:

答案 0 :(得分:1)

对于service.on('update') .pipe( tap(() => this.updateCounter(event)), switchMap(() => timer(5000)), ) .subscribe(() => this.timerHandler()); 来说,这看起来很简单:

Sub ytrewq()
    Dim Row3 As Long
    Row3 = 3
    Sheets("Quote Sheet").Cells(Row3, 2) = "     •     " & Sheets("CostSheet").Cells(46, 4).Value & "- Broan Model #QTXE90 Low Some Exhaust fan for"
End Sub

答案 1 :(得分:0)

什么不是:

const events$ = service.on('update');
events$.subscribe(this.updateCounter);
events$.delay(5000).subscribe(timerHandler);