I'm trying to make my website just with a JS file and in it I would like to know if a particular plugin is loaded since for example the carcel I only load in a page.
How do I test if the plugin is loaded to avoid getting errors on the console?
I'm trying to make my website just with a JS file and in it I would like to know if a particular plugin is loaded since for example the carcel I only load in a page.
How do I test if the plugin is loaded to avoid getting errors on the console?
You can create a recursive function that checks to see if certain globals are defined. If they are not, it calls itself with a wait time.
An example would look like this:
function ready(cb) {
if (typeof window.meuPlugin == 'undefined') return setTimeout(ready.bind(null, cb), 200);
else cb();
}
ready(function() { // esta callback será corrida quando a global estiver defenida
alert('Tudo está carregado!');
});
<script>
setTimeout(function() {
// para simular o carregamento
window.meuPlugin = function() {};
}, 2000)
</script>