Skip to content

Instantly share code, notes, and snippets.

@ben-ng
Last active August 29, 2015 14:19
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 ben-ng/297cc283f04638005048 to your computer and use it in GitHub Desktop.
Save ben-ng/297cc283f04638005048 to your computer and use it in GitHub Desktop.
setTimeout without setTimeout
/**
* 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