Skip to content

Instantly share code, notes, and snippets.

@schonstal
Created February 9, 2012 17:57
Show Gist options
  • Save schonstal/1781595 to your computer and use it in GitHub Desktop.
Save schonstal/1781595 to your computer and use it in GitHub Desktop.
Interval -- Magic?
object interval {
val list = collection.mutable.Map[ Class[_], Double ]()
/** Runs a block at a given interval */
def apply(t:Double, executeNow:Boolean=true)( block : =>Unit) {
val klass = (()=>{block}).getClass
if(list.isDefinedAt(klass)) {
val lastTime = list(klass)
if(util.getSeconds - lastTime >= t) {
block
list(klass) = util.getSeconds
}
}
else {
list( klass ) = util.getSeconds
if(executeNow) block
}
}
}
@schonstal
Copy link
Author

In your update loop, put:

interval(0.5) {
println("foo")
}

to print "foo" every 0.5s.

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