[英]setTimeout not working in injected JavaScript?
I have an external JavaScript file mypapopup.js with this content: 我有一个包含以下内容的外部JavaScript文件mypapopup.js :
function mypopup() {
alert("Hello stackoverflow")
}
In a Delphi XE8 VCL Form application, with TEmbeddedWB.ExecScript
I inject this JavaScript into a loaded document in EmbeddedWB: 德尔福XE8 VCL窗体应用程序,与TEmbeddedWB.ExecScript
我注入这个JavaScript到EmbeddedWB加载的文件:
procedure TForm1.btnPopupJSClick(Sender: TObject);
begin
EmbeddedWB1.ExecScript('var script = document.createElement(''script'');' +
'script.src = "mypapopup.js";' +
'script.setAttribute(''type'', ''text/javascript'');' +
'document.getElementsByTagName(''head'')[0].appendChild(script);' +
'setTimeout(mypopup(), 1000);'
,'JavaScript');
end;
Please note that with this code a script tag is being added to the HEAD section which references the external JavaScript file mypapopup.js
. 请注意,通过此代码,脚本标签被添加到HEAD部分,该脚本标签引用了外部JavaScript文件mypapopup.js
。
Then the mypopup
function from the external JavaScript file is called with a delay of 1000 ms. 然后,以1000 ms的延迟调用外部JavaScript文件中的mypopup
函数。
After clicking once the button btnPopupJS
in my Delphi program nothing happens. 在我的Delphi程序中单击一次btnPopupJS
按钮btnPopupJS
,什么也没有发生。
Only after clicking the button btnPopupJS
in my Delphi program a second time the JavaScript popup is executed WITHOUT A DELAY! 仅在第二次单击我的Delphi程序中的btnPopupJS
按钮btnPopupJS
,才执行JavaScript弹出窗口而没有延迟!
This is also the case when I increase the setTimeout
delay to eg 5000 ms, where also after having clicked the button a second time the popup is executed WITHOUT A DELAY. 当我将setTimeout
延迟增加到例如5000 ms时,也是这种情况,在第二次单击按钮之后,弹出菜单也没有延迟地执行。
So is there a way to wait until the external JavaScript has been loaded and then automatically execute the mypopup
function? 那么有没有办法等到外部JavaScript已加载然后自动执行mypopup
函数呢?
EDIT : I have found a solution, but I don't know whether this is an optimal solution: 编辑 :我找到了一个解决方案,但我不知道这是否是最佳解决方案:
procedure TForm1.btn1Click(Sender: TObject);
var
t, tt: Int64;
begin
EmbeddedWB1.ExecScript('var script = document.createElement(''script'');' +
'script.src = "mypapopup.js";' +
'script.setAttribute(''type'', ''text/javascript'');' +
'document.getElementsByTagName(''head'')[0].appendChild(script);'
// + 'setTimeout(mypopup, 1000);'
,'JavaScript');
t := TThread.GetTickCount;
repeat
tt := TThread.GetTickCount - t;
Application.ProcessMessages;
until tt > 1000;
EmbeddedWB1.ExecScript('mypopup();', 'JavaScript');
end;
I think there will be a reference error when executing setTimeout(mypopup, 1000);
我认为执行setTimeout(mypopup, 1000);
时会出现参考错误setTimeout(mypopup, 1000);
because the external js file isn't loaded yet so that mypopup
is unknown when executing that line. 因为尚未加载外部js文件,所以执行该行时mypopup
未知。
Please change 请更换
setTimeout(mypopup, 1000);
to 至
setTimeout(function() { mypopup(); }, 1000);
i also had a same problem. 我也有同样的问题。 I soluted this problem with this code: 我用以下代码解决了这个问题:
window.setTimeout(function(){ mypopup();},5000);
maybe it`s helpfull for you 也许对您有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.