我需要每秒减少输入值,然后打印出来。 当值达到 0 时,则变为 100,并重复递减过程。
例如:
Given value:
-> 234
Timer starts decreasing with every second
-> 233
-> 232
...
-> 1
-> 0 and the whole process repeats,
but starts with value 100
(and again decreasing, reaches 0, starting from 100)
我知道如何在 Rx 中使用计时器,但如何将它与描述的案例联系起来?
最佳答案
你只需要一个初始值如下的变量:
var start = 234
func startTimer() {
_ = Observable<Int>
.timer(0, period: 1, scheduler: MainScheduler.instance)
.subscribe(onNext: { _ in
self.start -= 1
if self.start <= 0 {
self.start = 100
}
print("Timer value>>>> : ",self.start)
})
}
关于swift - RxSwift 发出带有时间间隔的起始值并修改它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54004860/