簡體   English   中英

在特定條件下跳過/忽略來自事件訂閱的 Observable

[英]Skip/Ignore Observable fromEvent subscription on a specific condition

當條件為真時,我想跳過/忽略fromEvent() 可觀察對象訂閱:當this.currentPosition === this.vc.getScrollPosition()[1]我不想訂閱可觀察對象,因為它正在滾動到下一個直接項目,我的目標是等待用戶交互滾動到我界面上的下一個項目..

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;
            }
        }

    );

您可以在 pipe 中對其進行過濾。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM