Last active
August 29, 2015 14:15
-
-
Save mjlescano/d168c720eba3d1aa6670 to your computer and use it in GitHub Desktop.
Silly lib that executes a given callback when a variable is defined on window.
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
/** | |
* waitFor.js | |
* | |
* Silly lib that executes a given callback when a variable is defined on window. | |
* | |
* Matías Lescano | @mjlescano | |
* Licensed under the MIT license | |
*/ | |
;if( !window.waitFor ) (function(w){ | |
var rAF = w.requestAnimationFrame | |
|| w.mozRequestAnimationFrame | |
|| w.webkitRequestAnimationFrame | |
|| w.msRequestAnimationFrame | |
|| function(f){ return setTimeout(f, 25) } | |
var watching = false | |
var bars = [] | |
function watch(){ | |
var i = bars.length | |
while( i-- ){ | |
var item = bars[i] | |
if( window[item[0]] !== undefined ){ | |
bars.splice(i, 1) | |
item[1].call(item[1]) | |
} | |
} | |
watching = bars.length ? rAF(watch) : false | |
} | |
window.waitFor = function waitFor(bar, cb){ | |
bars.unshift([bar, cb]) | |
if( !watching ) watching = rAF(watch) | |
} | |
})(window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment