Last active
July 11, 2020 22:05
-
-
Save nbeloglazov/9633318 to your computer and use it in GitHub Desktop.
setTimeout, clearTimeout, setInterval, clearInterval implementations for Rhino that works in 1.7R4
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
var setTimeout, clearTimeout, setInterval, clearInterval; | |
(function () { | |
var executor = new java.util.concurrent.Executors.newScheduledThreadPool(1); | |
var counter = 1; | |
var ids = {}; | |
setTimeout = function (fn,delay) { | |
var id = counter++; | |
var runnable = new JavaAdapter(java.lang.Runnable, {run: fn}); | |
ids[id] = executor.schedule(runnable, delay, | |
java.util.concurrent.TimeUnit.MILLISECONDS); | |
return id; | |
} | |
clearTimeout = function (id) { | |
ids[id].cancel(false); | |
executor.purge(); | |
delete ids[id]; | |
} | |
setInterval = function (fn,delay) { | |
var id = counter++; | |
var runnable = new JavaAdapter(java.lang.Runnable, {run: fn}); | |
ids[id] = executor.scheduleAtFixedRate(runnable, delay, delay, | |
java.util.concurrent.TimeUnit.MILLISECONDS); | |
return id; | |
} | |
clearInterval = clearTimeout; | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment