Wait for global script to load

30/10/2023

A helper function that waits for a script with a global identifier to load.

function waitFor(item, callback) {
    if (window[item]) {
        callback()
    } else {
        setTimeout(function () {
            waitFor(item, callback)
        }, 100)
    }
}

It can be used like this:

waitFor('identifier', () => {
    // 'identifier' is available here
    console.log(identifier)
})