Skip to content

Instantly share code, notes, and snippets.

@mjlescano
Last active August 29, 2015 14:15
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 mjlescano/d168c720eba3d1aa6670 to your computer and use it in GitHub Desktop.
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.
/**
* 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