RxSwift 操作符 (debounce)

debounce

ReactiveX:
only emit an item from an Observable if a particular timespan has passed without it emitting another item

在指定的时间内,只接受最新的数据。

let pb1 = PublishSubject<Int>()
pb1.debounce(2, scheduler: MainScheduler.instance)
    .subscribe(onNext: { int in
        print("element:", int)
    })
    .disposed(by: bag)
pb1.onNext(1)
pb1.onNext(2)
pb1.onNext(3)
pb1.onNext(4)
pb1.onNext(5)

输出:
element: 5

指定了两秒钟,所以在两秒钟以内,只接收了到了最新的element: 5

猜你喜欢

转载自blog.csdn.net/weixin_38318852/article/details/80334770