Created
December 23, 2015 20:00
-
-
Save miksansegundo/09c844cb0df7258de76d to your computer and use it in GitHub Desktop.
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
// 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