Skip to content

Instantly share code, notes, and snippets.

@kozy4324
Created March 5, 2012 13:29
Show Gist options
  • Save kozy4324/1978304 to your computer and use it in GitHub Desktop.
Save kozy4324/1978304 to your computer and use it in GitHub Desktop.
Rhinoでタイマー系関数をエミュレートする
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