Skip to content

Instantly share code, notes, and snippets.

@kayhadrin
Last active February 28, 2023 17:52
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kayhadrin/4bf141103420b79126e434dfcf6d4826 to your computer and use it in GitHub Desktop.
Save kayhadrin/4bf141103420b79126e434dfcf6d4826 to your computer and use it in GitHub Desktop.
Nashorn setTimeout polyfill
/**
* js-timeout-polyfill
* @see https://blogs.oracle.com/nashorn/entry/setinterval_and_settimeout_javascript_functions
*/
(function (global) {
'use strict';
if (global.setTimeout ||
global.clearTimeout ||
global.setInterval ||
global.clearInterval) {
return;
}
var Timer = global.Java.type('java.util.Timer');
function toCompatibleNumber(val) {
switch (typeof val) {
case 'number':
break;
case 'string':
val = parseInt(val, 10);
break;
case 'boolean':
case 'object':
val = 0;
break;
}
return val > 0 ? val : 0;
}
function setTimerRequest(handler, delay, interval, args) {
handler = handler || function () {
};
delay = toCompatibleNumber(delay);
interval = toCompatibleNumber(interval);
var applyHandler = function () {
handler.apply(this, args);
};
/*var runLater = function () {
Platform.runLater(applyHandler);
};*/
var timer;
if (interval > 0) {
timer = new Timer('setIntervalRequest', true);
timer.schedule(applyHandler, delay, interval);
} else {
timer = new Timer('setTimeoutRequest', false);
timer.schedule(applyHandler, delay);
}
return timer;
}
function clearTimerRequest(timer) {
timer.cancel();
}
/////////////////
// Set polyfills
/////////////////
global.setInterval = function setInterval() {
var args = Array.prototype.slice.call(arguments);
var handler = args.shift();
var ms = args.shift();
return setTimerRequest(handler, ms, ms, args);
};
global.clearInterval = function clearInterval(timer) {
clearTimerRequest(timer);
};
global.setTimeout = function setTimeout() {
var args = Array.prototype.slice.call(arguments);
var handler = args.shift();
var ms = args.shift();
return setTimerRequest(handler, ms, 0, args);
};
global.clearTimeout = function clearTimeout(timer) {
clearTimerRequest(timer);
};
global.setImmediate = function setImmediate() {
var args = Array.prototype.slice.call(arguments);
var handler = args.shift();
return setTimerRequest(handler, 0, 0, args);
};
global.clearImmediate = function clearImmediate(timer) {
clearTimerRequest(timer);
};
})(this);
@lisonge
Copy link

lisonge commented Apr 4, 2021

you can use requestAnimationFrame or animationend

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment