簡體   English   中英

使用scala.rx中的Timer

[英]Using Timer from scala.rx

有人可以給我一個簡單的例子,使用李浩義的scala.rx中的Timer,它不依賴於Akka或scalajs,dom和rx之外的任何其他庫嗎?

Haoyi的GitHub定時器的例子是:

import scala.concurrent.duration._
implicit val scheduler = new AkkaScheduler(akka.actor.ActorSystem())

val t = Timer(100 millis)
var count = 0  
val o = Obs(t){
  count = count + 1
}

println(count) // 3
println(count) // 8
println(count) // 13

但是,這使用了Akka。

查看scala.rx api ,創建rx.ops.Timer的方法是:

new Timer(interval: FiniteDuration, delay: FiniteDuration)(implicit scheduler: Scheduler, p: Propagator[P], ec: ExecutionContext)

Scheduler是一個定義為的特征:

abstract def scheduleOnce[T](interval: FiniteDuration)(thunk: ⇒ T)(implicit executor: ExecutionContext): Unit

Scheduler是JVM上的Akka ActorSystem和JavaScript中的setTimeout函數。“

雖然api中的所有信息都很有用,但我仍然無法為簡單的計時器獲得正確的語法。

如果我從文檔中正確理解,您只需在范圍內提供隱式DomScheduler而不是AkkaScheduler

import rx.ops._

implicit val scheduler = new DomScheduler

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM