Created
January 19, 2014 21:19
-
-
Save fonzerelly/8511168 to your computer and use it in GitHub Desktop.
timer functions for Rhino taken from http://stackoverflow.com/questions/2261705/how-to-run-a-javascript-function-asynchronously-without-using-settimeout ammended by endAllTimers
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, | |
endAllTimers; | |
(function () { | |
var timer = new java.util.Timer(); | |
var counter = 1; | |
var ids = {}; | |
setTimeout = function (fn,delay) { | |
var id = counter += 1; | |
ids[id] = new JavaAdapter(java.util.TimerTask,{run: fn}); | |
timer.schedule(ids[id],delay); | |
return id; | |
}; | |
clearTimeout = function (id) { | |
ids[id].cancel(); | |
timer.purge(); | |
delete ids[id]; | |
}; | |
setInterval = function (fn,delay) { | |
var id = counter += 1; | |
ids[id] = new JavaAdapter(java.util.TimerTask,{run: fn}); | |
timer.schedule(ids[id],delay,delay); | |
return id; | |
}; | |
clearInterval = clearTimeout; | |
/*** added ***/ | |
endAllTimers = function () { | |
timer.cancel(); | |
}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment