Created
March 5, 2012 13:29
-
-
Save kozy4324/1978304 to your computer and use it in GitHub Desktop.
Rhinoでタイマー系関数をエミュレートする
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
if !window? && !setTimeout? && Packages? && print? # you are rhino | |
hash = {} | |
setTimeoutBase = (once) -> | |
(fn, delay) -> | |
fn = new Function fn if typeof fn=="string" | |
delay = +delay || 0 | |
thread = spawn -> | |
Packages.java.lang.Thread.sleep 15 # if delay is 0, can't access [tid] | |
while hash[tid] | |
Packages.java.lang.Thread.sleep delay | |
fn() if hash[tid] | |
hash[tid] = false if once | |
undefined | |
tid = thread.getName() | |
hash[tid] = true | |
tid | |
clearTimeout = (tid) -> hash[tid] = false if hash[tid] | |
this.setTimeout = setTimeoutBase true | |
this.clearTimeout = clearTimeout | |
this.setInterval = setTimeoutBase false | |
this.clearInterval = clearTimeout | |
this.console = log:(str)->print str | |
this.window = this |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment