Skip to content

Instantly share code, notes, and snippets.

@styoe
Created March 5, 2020 21:09
Show Gist options
  • Save styoe/752ccac8a7f46a13cb0eb8b4c977771e to your computer and use it in GitHub Desktop.
Save styoe/752ccac8a7f46a13cb0eb8b4c977771e to your computer and use it in GitHub Desktop.
<script>
(function () {
var store = []
var timeout = null
function checkDepsHaveLoadedForItem (item) {
for(let i=0; i<item.deps.length; i++) {
if(!window[item.deps[i]]) return false
}
return true
}
function checkStore () {
for(var i=store.length-1; i>=0; i--) {
var ready = checkDepsHaveLoadedForItem(store[i])
if(ready) {
store[i].cb()
store.splice(i, 1)
}
}
if(store.length) {
timeout = setTimeout(checkStore, 500)
} else {
timeout = null
}
}
window.runThisAfterScriptsLoad = function (cb, deps) {
store.push({cb: cb, deps: deps})
if(!timeout) {
checkStore()
}
}
})()
</script>
<script>
runThisAfterScriptsLoad(function () {
console.log("https://www.youtube.com/watch?v=tFlAFo78xoQ")
},
['jQuery'])
runThisAfterScriptsLoad(function () {
console.log("This shall never be seen!")
},
['theVarThatNeverGetsDefined'])
</script>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment