问题
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