从纯 JavaScript 中,我们可以直接测试功能;看: Reliably detect if the script is executing in a web worker .
您将如何通过 Scala.js 完成此任务?
最佳答案
正如 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")
}
我希望这会有所帮助。
关于scala - 在 Scala.js 中,代码如何检测它是在浏览器窗口中运行还是在 WebWorker 中运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55132480/