Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save miksansegundo/09c844cb0df7258de76d to your computer and use it in GitHub Desktop.
Save miksansegundo/09c844cb0df7258de76d to your computer and use it in GitHub Desktop.
// setTimeout(callback, 0)
// 0 == 5 ms, so is not a Zero delay
// The next postpone function is fast
// Use: postpone(callback)
var postpone = (function () {
var fnMap = new Map(), idMap = new Map(), fnId = 0;
var msg = { fnId: 0 };
function _postpone(fn) {
if (!fnMap[fn]) {
fnId++;
fnMap[fn] = fnId;
idMap[fnId] = fn;
}
msg.fnId = fnMap[fn];
postMessage(msg, '*');
}
function _postponeListener(e) {
var fnId = e.data.fnId;
if (fnId) idMap[fnId]();
}
window.addEventListener("message", _postponeListener);
return _postpone;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment