Created
February 9, 2012 17:57
-
-
Save schonstal/1781595 to your computer and use it in GitHub Desktop.
Interval -- Magic?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In your update loop, put:
interval(0.5) {
println("foo")
}
to print "foo" every 0.5s.