swift - RxSwift 发出带有时间间隔的起始值并修改它

标签 swift timer observable rx-swift

我需要每秒减少输入值,然后打印出来。 当值达到 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/

相关文章:

xcode - 我在 xcode 的 viewcontroller.swift 中的一个按钮中编码 - 如何让它显示在 Storyboard 中?

swift - 向 wit.ai 发送文本时接收结果的问题

linux - 让 ./countdown 5 从 5 开始倒计时

javascript - 如何检查数据是否完整加载以使用异步管道Angular 8

javascript - 为什么 Knockout.js 数据绑定(bind)到 checkValue 会覆盖 observable?

ios - UIImagePicker Controller swift

ios - 为什么之前调用 reloadRows 时 UITableView setContentOffset 无法正常工作?

javascript - 一个准确的 Javascript 计时器

c# - 如何为个人用户创建登录 session 计时器

angular - 在发出两个可观察对象的第一个值后,Zip 不发出值