admin管理员组文章数量:1333424
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 bees available. The above code does not work but I am unsure as to how to resolve it.
Regards Paul
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 bees available. The above code does not work but I am unsure as to how to resolve it.
Regards Paul
Share Improve this question edited May 23, 2017 at 10:24 CommunityBot 11 silver badge asked Apr 26, 2012 at 7:19 Paul MarsdenPaul Marsden 15111 bronze badges4 Answers
Reset to default 5There 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.
function waitForFn(fnName, args){
var fn;
try{
eval("fn = " + fnName);
if(fn){
fn.apply(null, args);
}else{
setTimeout(function(){waitForFn(fnName, args);}, 50);
}
}catch(e){
setTimeout(function(){waitForFn(fnName, args);}, 50);
}
}
waitForFn("Portal.Management_Init", [arg0, arg1]);
Basically, when this line of code is executed: window.setTimeout(waitForFnc(fnc), 50);
, the "waitForFnc" is evaluated before the timeout is set. While you need to pass the calling statement as a parameter.
Here's how you do that:
window.setTimeout(function() {waitForFnc(fnc);}, 50);
What this does, it defines a function, the same way as if you'd write it into the variable:
var myFunc = function() {
waitForFnc(fnc);
};
This function is not yet executed, it is only defined. Then you pass it into the "setTimeout":
window.setTimeout(myFunc, 50);
Which makes the "setTimeout" to execute that function after 50msec. And when it does, it will call waitForFnc(fnc)
.
In your code replace:
window.setTimeout(waitForFnc(fnc), 50);
with closure:
window.setTimeout(function() {waitForFnc(fnc)}, 50);
But may I ask why do you need such a weird code? I would rather expect to have an API allowing to register some callback:
Portal.onManagementInitAvailable(fn);
Depending on what you're loading, you might be able to leverage require.js to take care of this for you; that's basically what it's for: http://requirejs/docs/why.html#9
本文标签: closuresI need a generic javascript quotwait for function to be availablequotStack Overflow
版权声明:本文标题:closures - I need a generic javascript "wait for function to be available" - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742311156a2450899.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论