-
-
Save styoe/752ccac8a7f46a13cb0eb8b4c977771e to your computer and use it in GitHub Desktop.
This file contains 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
<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