delphi - setTimeout not working in injected JavaScript? -
i have external javascript file mypapopup.js content:
function mypopup() { alert("hello stackoverflow") }
in delphi xe8 vcl form application, tembeddedwb.execscript
inject javascript loaded document in 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 code script tag being added head section references external javascript file mypapopup.js
.
then mypopup
function external javascript file called delay of 1000 ms.
after clicking once button btnpopupjs
in delphi program nothing happens.
only after clicking button btnpopupjs
in delphi program second time javascript popup executed without delay!
this case when increase settimeout
delay e.g. 5000 ms, after having clicked button second time popup executed without delay.
so there way wait until external javascript has been loaded , automatically execute mypopup
function?
edit: have found solution, don't know whether 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 reference error when executing settimeout(mypopup, 1000);
because external js file isn't loaded yet mypopup
unknown when executing line.
please change
settimeout(mypopup, 1000);
to
settimeout(function() { mypopup(); }, 1000);
Comments
Post a Comment