Last active
August 29, 2015 13:58
-
-
Save imyelo/10251164 to your computer and use it in GitHub Desktop.
wait window[obj]
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
var wait = function (name, context) { | |
context = context || window; | |
var isReady = false; | |
var tasks = []; | |
var when = function (callback) { | |
if (isReady) { | |
return callback(); | |
} | |
return tasks.push(callback); | |
}; | |
var check = function (next) { | |
var again = function () { | |
console.log('check'); | |
setTimeout(function () { | |
if (typeof context[name] === 'undefined') { | |
again(); | |
} else { | |
next(); | |
} | |
}, 200); | |
}; | |
again(); | |
}; | |
check(function () { | |
isReady = true; | |
while (tasks.length > 0) { | |
tasks.shift()(); | |
} | |
}); | |
return when; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment