Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Created February 14, 2017 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattpodwysocki/9839fa6d54673aa6a766d3f6a7a7e927 to your computer and use it in GitHub Desktop.
Save mattpodwysocki/9839fa6d54673aa6a766d3f6a7a7e927 to your computer and use it in GitHub Desktop.
(function (global) {
'use strict';
if (global.setTimeout) { return; }
let nextId = 1;
let tasksById = {};
function setTimeout(fn: () => void, ms: number) {
tasksById[nextId] = fn;
registerSetTimeout(nextId, ms);
return nextId++;
}
function clearTimeout(id: number) {
delete tasksById[id];
}
function runIfPresent(id: number) {
let task = tasksById[id];
if (task) {
try {
task();
} finally {
clearTimeout(id);
}
}
}
function registerSetTimeout(id: number, ms: number) {
global.runInBackground(() => {
global.pause(ms);
runIfPresent(id);
});
}
global.setTimeout = setTimeout;
global.clearTimeout = clearTimeout;
}(control));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment