RxSwift 操作符 (throttle)

throttle

Throttle.swift:
Returns an Observable that emits the first and the latest item emitted by the source Observable during sequential time windows of a specified duration.

 This operator makes sure that no two elements are emitted in less then dueTime.

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

let pb1 = PublishSubject<Int>()
pb1.throttle(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: 1
element: 5

指定了两秒钟,所以在两秒钟以内,只接收了第一条和最新的数据。
适用于:输入框搜索限制发送请求。

猜你喜欢

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