Skip to content

Instantly share code, notes, and snippets.

@oswaldo
Last active January 30, 2017 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oswaldo/0fe6bb55e56c3c918b4ab8780a512e93 to your computer and use it in GitHub Desktop.
Save oswaldo/0fe6bb55e56c3c918b4ab8780a512e93 to your computer and use it in GitHub Desktop.
Simple ScalaJS "Speed Reader" example
//https://scalafiddle.io/sf/55sE0OL/10
import scalatags.JsDom.all._
import org.scalajs.dom
import fiddle.Fiddle.panel
val wpm = 300d
val group = 1
val interval = 60 / wpm * group * 1000
val message = """Speed reading is any of several techniques used to improve one's ability to read quickly.
Speed reading methods include chunking and minimizing subvocalization.
The many available speed reading training programs include books, videos, software, and seminars."""
val tokens = message.split("\\s+").grouped(group).map(_.mkString(" ")).toList ++ List("")
def start(again: Boolean = false): Unit = println(button((if (again) "Restart" else "Start"), onclick := { () =>
var i = 0
var intervalHandle = 0
intervalHandle = dom.window.setInterval(() => {
while (panel.childElementCount > 0)
panel.removeChild(panel.firstChild)
if (i < tokens.size) {
panel.appendChild(
div(
padding := "10px",
id := "page",
h1(
margin := "auto",
textAlign := "center",
tokens(i))).render)
i = i + 1
} else {
dom.window.clearInterval(intervalHandle)
start(again = true)
}
}, interval)
}))
start()
@oswaldo
Copy link
Author

oswaldo commented Jan 30, 2017

updated to current scalafiddle.io

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment