Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the ScalaJS way to make an event occur in n milliseconds time?

Tags:

scala.js

Is it to use ScalaJS DOM and use the following?

org.scalajs.dom.setTimeout( () => {
  // Work
}, 1000)

Is there another way or a better way within the context of ScalaJS?

like image 474
Phil Avatar asked Feb 04 '15 08:02

Phil


People also ask

What is Scalajs?

Scala. js is a compiler that compiles Scala source code to equivalent Javascript code. That lets you write Scala code that you can run in a web browser, or other environments (Chrome plugins, Node.

Is Scala JavaScript?

Scala. js lets you write Scala code that is compiled to JavaScript code that can then be used in the browser. The approach is similar to TypeScript, ReScript, and other languages that are compiled to JavaScript. Note that although Scala is a type-safe language, no types are declared in the above code.

Why Scala js?

With Scala. js, you can cross compile your Scala code to a JavaScript executable that can run on all major web browsers. You get all the benefits of the web platform in terms of deployability, security, and hyperlinking, with none of the problems of writing your software in JavaScript. Scala.


2 Answers

Starting with Scala.js 0.6.0, there is a more standard way, and more idiomatic Scala, to do it:

import scala.scalajs.js.timers._

setTimeout(1000) { // note the absence of () =>
  // work
}

See the ScalaDoc of timers.

like image 54
sjrd Avatar answered Sep 21 '22 13:09

sjrd


There isn't a better way. If you want you can wrap it in a helper and call it whatever you want, but by default that's it.

like image 28
Li Haoyi Avatar answered Sep 21 '22 13:09

Li Haoyi