Last active
August 29, 2015 14:19
-
-
Save ben-ng/297cc283f04638005048 to your computer and use it in GitHub Desktop.
setTimeout without setTimeout
This file contains hidden or 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
/** | |
* Just call __PBSJC_drain at the end of your code | |
*/ | |
var __PBSJC_funcHandles = [] | |
, __PBSJC_funcHandleCount = 0 | |
, __PBSJC_drain = function __PBSJC_drain () { | |
var extracted, i, ii, cur | |
while (__PBSJC_funcHandles.length) { | |
extracted = __PBSJC_funcHandles | |
__PBSJC_funcHandles = [] | |
extracted.sort(function (a, b) { | |
return a[1] - b[1] | |
}) | |
for (i=0, ii=extracted.length; i<ii; ++i) { | |
cur = extracted[i] | |
if (cur[3] === true) { | |
__PBSJC_funcHandles.push(cur) | |
} | |
cur[0]() | |
} | |
} | |
} | |
, __PBSJC_clearFunc = function __PBSJC_clearFunc (handle) { | |
var i, ii | |
for (i=0, ii=__PBSJC_funcHandles.length; i<ii; ++i) { | |
if (__PBSJC_funcHandles[i][2] === handle) { | |
__PBSJC_funcHandles.splice(i, 1) | |
return | |
} | |
} | |
} | |
, __PBSJC_queueFunc = function __PBSJC_queueFunc (func, delay) { | |
__PBSJC_funcHandles.push([func, delay, __PBSJC_funcHandleCount++, false]) | |
} | |
, __PBSJC_queueIntv = function __PBSJC_queueFunc (func, delay) { | |
__PBSJC_funcHandles.push([func, delay, __PBSJC_funcHandleCount++, true]) | |
} | |
, setTimeout = __PBSJC_queueFunc | |
, setImmediate = __PBSJC_queueFunc | |
, setInterval = __PBSJC_queueIntv | |
, clearTimeout = __PBSJC_clearFunc | |
, clearInterval = __PBSJC_clearFunc | |
, clearImmediate = __PBSJC_clearFunc | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment