countdown timer by `RxSwift`

孤者浪人 提交于 2020-06-16 21:52:18

问题


I need a thirty-second time counter with RxSwift. This is a duplicate question but there is no clear answer to the questions


回答1:


Better approach for existing answer.

let countDown = 15 // 15 seconds 
Observable<Int>.timer(.seconds(0), period: .seconds(1), scheduler: MainScheduler.instance)
        .take(countDown+1)
        .subscribe(onNext: { timePassed in
            let count = self.countDown - timePassed
            print(count)

        }, onCompleted: {
            print("count down complete")
        })



回答2:


With 5.0 version of RxSwift you can do:

    Observable<Int>.interval(.seconds(30), scheduler: MainScheduler.instance).bind { timePassed in

    }.disposed(by: yourDisposeBag)


来源:https://stackoverflow.com/questions/58047235/countdown-timer-by-rxswift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!