【发布时间】:2019-11-27 18:38:21
【问题描述】:
这是我的代码:
app.component.ts
notifier$ = new BehaviorSubject<any>({});
notify() {
this.notifier$.next({});
}
app.component.html
<div (scroll)="notify()"></div>
<child-component [inp]="notifier$ | async" />
问题是当用户滚动时,notify() 函数会被重复调用,而我只想在每次用户开始滚动时调用notify() 一次。
我可以通过这种方式完成我想要的:
scrolling = false;
scrollingTimer: NodeJS.Timer;
notify() {
clearTimeout(this.scrollingTimer);
if (!this.scrolling) {
this.notifier$.next({});
}
this.scrolling = true;
this.scrollingTimer = setTimeout(() => (this.scrolling = false), 1000);
}
但我希望喜欢使用rxjs 进行此操作。然而debounceTime 与我想要的相反,throttleTime 和auditTime 也不是我想要的。有没有办法做到这一点?
【问题讨论】:
-
用户开始滚动,因此调用了
notify(),然后您想忽略其他调用,直到调用停止 1 秒? -
@Reactgular 是的
标签: javascript angular rxjs observable