在Scala.js中,代码如何检测它是在浏览器窗口还是在WebWorker中运行?

问题描述 投票:1回答:1

从纯JavaScript,我们可以直接测试功能;见:Reliably detect if the script is executing in a web worker

你会如何从Scala.js实现这一目标?

scala web-worker scala.js
1个回答
2
投票

正如Justin du Coeur已经评论过的那样,因为ScalaJS编译成JavaScript,所以你无法在JavaScript中做任何你在ScalaJS中无法做到的事情。

因此,您会发现代码看起来与您链接的代码非常相似:

import org.scalajs.dom

if(js.typeOf(dom.document) == "undefined") {
  println("I'm fairly confident I'm a webworker")
} else {
  println("I'm fairly confident I'm in the renderer thread")
}

Try it out!


我希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.