【问题标题】:Skip/Ignore Observable fromEvent subscription on a specific condition在特定条件下跳过/忽略来自事件订阅的 Observable
【发布时间】:2020-12-16 09:12:43
【问题描述】:

当条件为真时,我想跳过/忽略 订阅 fromEvent() observable: 当this.currentPosition === this.vc.getScrollPosition()[1] 我不想订阅 observable 因为它直接滚动到下一个项目,我的目标是等待用户交互滚动到我界面上的下一个项目..

fromEvent(window, 'scroll').pipe(
    distinctUntilChanged(),
    debounceTime(1000)
)
    .subscribe(
        (event) => {

            const current = this.positions.find(e => e.name === this.currentSectionName);
            const indexOfCurrent = this.positions.indexOf(current);

            if (indexOfCurrent + 1 === this.positions.length) {
                // stay in the same position if it's the last item
                this.vc.scrollToPosition([0, current.position]);
                this.currentPosition = current.position;

            } else {
                // move to next position if it's not the last item
                const next = this.positions[indexOfCurrent + 1];
                this.vc.scrollToPosition([0, next.position]);
                this.currentPosition = next.position;
            }
        }

    );

【问题讨论】:

    标签: javascript angular typescript rxjs


    【解决方案1】:

    你可以在管道中过滤它。

    fromEvent(window, 'scroll').pipe(
        filter( () => this.currentPosition !== this.vc.getScrollPosition()[1] ),
        distinctUntilChanged(),
        debounceTime(1000)
    )
    

    【讨论】:

      猜你喜欢
      • 2021-09-02
      • 2019-10-04
      • 2020-01-30
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多