Javascript Rxjs是否在订阅处理程序中创建计时器?
我订阅了一个可观测对象,我正在尝试在可观测对象的最后一个发射值五秒钟后运行计时器处理程序。然后在下一次发射时重置计时器Javascript Rxjs是否在订阅处理程序中创建计时器?,javascript,rxjs,Javascript,Rxjs,我订阅了一个可观测对象,我正在尝试在可观测对象的最后一个发射值五秒钟后运行计时器处理程序。然后在下一次发射时重置计时器 // 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) =>
// 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
}
这很好,但我想知道是否有一种更符合Rxjs模式的方法
const events$ = service.on('update');
events$.subscribe(this.updateCounter);
events$.delay(5000).subscribe(timerHandler);
对于
switchMap()
,这看起来是一项简单的任务:
你好这会延迟()取消先前的延迟,例如退格吗?不。如果这是你需要的,你应该考虑使用DeBung。不利的一面是,它可能导致“饥饿”,所以你应该混合去盎司和超时,以确保你最终更新
service.on('update')
.pipe(
tap(() => this.updateCounter(event)),
switchMap(() => timer(5000)),
)
.subscribe(() => this.timerHandler());