gpt4 book ai didi

javascript - 如何在 rxjs 中组合 Scheduler.animationFrame 和 fromEvent(window, 'scroll' ) 流?

转载 作者:行者123 更新时间:2023-11-28 11:46:56 25 4
gpt4 key购买 nike

我有两个可观察对象Observable.of(0,animationFrame)Observable.fromEvent(window, 'scroll')。我想将它们组合起来,以便仅在animationFrame 的刻度上调用我的 renderScrollBar(event)

  Observable.fromEvent(window, 'scroll')
.map((event: any) => event.currentTarget)
.subscribe((event) => {
this._renderScrollBar(event);
});
let x = 0;
Observable.of(0, animationFrame)
.repeat()
.takeUntil(Observable.timer(1000))
.subscribe(() => console.log(x++));
}

最佳答案

如果您想限制 requestAnimationFrame 的滚动事件,您可以使用 throttleTime(0,animationFrame) 运算符而不是 Observable.of(0,animationFrame)

示例

Observable.fromEvent(window, 'scroll')
.throttleTime(0, animationFrame)

更新:

RxJS 6

import { fromEvent, animationFrameScheduler } from "rxjs";
import { throttleTime } from "rxjs/operators";

fromEvent(window, "scroll")
.pipe(throttleTime(0, animationFrameScheduler))

关于javascript - 如何在 rxjs 中组合 Scheduler.animationFrame 和 fromEvent(window, 'scroll' ) 流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46621557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com