javascript - 在特定条件下跳过/忽略来自事件订阅的 Observable
问题描述
当条件为真时,我想跳过/忽略 对fromEvent() observable的订阅:当我不想订阅 observable 因为它直接滚动到下一个项目并且我的目标是等待用户交互滚动到下一个我的界面上的项目..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;
}
}
);
解决方案
您可以在管道中过滤它。
fromEvent(window, 'scroll').pipe(
filter( () => this.currentPosition !== this.vc.getScrollPosition()[1] ),
distinctUntilChanged(),
debounceTime(1000)
)
推荐阅读
- coq - (1/2)*(xy)^2 在 x 上的微分是 x - y
- windows - CMD 迭代并递归重命名所有文件名并将当前子目录文件夹附加到当前文件名
- python - 如何将分隔值转换为单热编码列?
- node.js - Express 节点 js webssockets 正在从 websocket 服务器接收消息但无法发送它们
- javascript - 使用 gpu.js 的错误值
- python - 如果 datetime 对象大于某个日期,则 for 循环
- javascript - div中的MathJax高度不正确
- python - 选择查询问题 (Pyodbc)
- mysql - SQL:查询以获取用户在给定月份、年份中至少存在 1 天的行
- html - 无法使用 `overflow:none` 和 `-webkit-scrollbar` 隐藏滚动条