How to execute a script synchronously?
How to execute a script synchronously?
I read in release notes for 9.9.10 that the script engine is now synchronous.
But still when i.e. called LL.RunScript(“script1”,null); LL.RunScript(“script2”,null); both are executed asynchronous on parallel. How to solve if script2 requires script1 to be finished?
I tried something like setTimeout(new function() { LL.RunScript(“script1”,arg1); },1000); setTimeout(new function() { LL.RunScript(“script2”,arg2); },2000);, but both the anonymous function does not seem to work, the script are executed immediately, not with delay.
function RunScript1() { LL.RunScript(“script1”,null); };setTimeout(Runscript1,1000); does work with delay, but it is not possible to pass arguments.
Any ideas?
]]>
< ![CDATA[
why don’t you pass arguments to runscript instead of null. And run script2 from script1
]]>
< ![CDATA[
https://plus.google.com/u/0/+gerdreuter/posts/GTePkxuE8xv
]]>
< ![CDATA[
Bogdan Tautu If all routines are just comprised of rountine1, routine2, then yours might be right.
But if there are some routines(3) next to routine2, and those use the results from 1 or/and 2, then yours doesnt work.
Because current LLs runScript is exactly async and, morever unreasonably routine3 is run first, and then runScript1,2.
I think the ONLY method is just embedding all if we want synchrous result.
]]>
< ![CDATA[
routine0
LL.RunScript(“routine1”,null); LL.RunScript(“routine2”,null);
routine3…
–>routine0 -> routine3 -> routine1/routine2
It’s my test result.
]]>
< ![CDATA[
Thank you all for sharing.
Looks like KyungJoon Lee is right about the order of execution.
To be more specific, what I want/need to achieve in my case is: I iterate through a list of items, for each via RunScript I wanted to call an existing script that animates one item. Only if this has reached it’s final position the next item’s animation can be started.
Looks like I have to copy the code from the existing script over.
Hope that works. Still might face the problem that animated panel scrolling also is executed asynchronously as it seem.
]]>
< ![CDATA[
I still think that you could use the data parameter of runscript to chain the script execution.
KyungJoon Lee in your example you could move routine3 to another script and call it using RunScript.
Example (not tested):
var list = [“routine2″,”routine3”];
LL.RunScript(“routine1”, JSON.stringify(list));
And at the end of routine1 and routine2 write:
var data = LL.getEvent().getData();
if (!data) return;
var list = JSON.parse(data);
if (!list.length) return;
var script = list.shift();
LL.RunScript(script, JSON.stringify(list));
]]>
< ![CDATA[
The previous script engine was asynchronous in the sense that scripts were executed on another thread. The current one executes on the UI thread. Still scripts can be paused and resumed and as a consequence it may be possible to interleave script execution, this is not really asynchronous.
All this is stuff is not good.
I guess that running script is not what you need, what you need is to keep your code modular, call various sub routines and keep things clean.
Javascript is by essence not a good language for this (big JS things tend to be monolithic) and LL clearly does not help.
One thing is almost 100% certain: running script as sub routines will never work well. Instead an include mechanism should replace (or complement) this function, a bit like in html pages after all. Included script should not run code themselves, but define functions that can latter be called, synchronously, with arguments and a return value and so on.
But implementing an include mechanism is a bit problematic. It can be done, but these days my free time is… 0 🙁
]]>
< ![CDATA[
Bogdan Tautu I think your idea is half valid, half unrealistic.
If we use all runScript()s in the LAST line of each scripts, then your are right, because all runScripted routines are processed after doing routines in parents script.
But it is just like spliting long lines of routine into serial pieces. I think it has no merit we use runScript…
]]>
< ![CDATA[
Bogdan Tautu Tried that, but as KyungJoon Lee said, all runScripts are executed as almost parallel directly after calling script exits. Does not help.
And thanks, Pierre Hébert for the explanation, helped a lot understanding. No problem, will do what I have in mind other way.
]]>