I have found this excellent snippet Make my userscript wait for other scripts to load that shows me how to wait for a function to be available before calling it.
Currently I have this local code in my script which I have put together which works for me
waitForFnc();
function waitForFnc() {
if (typeof Portal.Management_Init == "undefined") {
window.setTimeout(waitForFnc, 50);
}
else {
Portal.Management_Init();
}
}
However, I would like to write a generic version of ‘waitForFnc’ as I need to do the same thing in several places. Something like
waitForFnc(Portal.Management_Init);
function waitForFnc(fnc) {
if (typeof fnc == "undefined") {
window.setTimeout(waitForFnc(fnc), 50);
}
else {
fnc();
}
}
where I pass the name of the function in which is called when it becomes available. The above code does not work but I am unsure as to how to resolve it.
Regards
Paul
There are some potential problems with what you are trying to do. If you call waitForFnc() before Portal is even defined, you will get a null property access exception. If you are trying for a truly generic solution, you will probably have to use eval() *gasp*
While we’re at it, let’s add support for passing arguments to the function we’re waiting on.