[英]Indesign and Extendscript: Multiple functions, loops, and timing. Function called in a loop, not in sync
關鍵問題是循環中的函數不同步。 除非我包含 alert() 來減慢進程,否則我不會得到正確的var myDimensions
。 我不確定是否需要使用回調。 我閱讀了它們並發現它們令人困惑,我想避免回調地獄。 發生的情況是,在循環中運行函數時,我的其他一些函數調用不同步。 我正在將此 JavaScript 編寫為用於 Adobe InDesign 的 Extendscript,因此 Extendscript 不包含現代 JS。
在函數中添加一個alert()
以返回值以確認我獲得了正確的值后,我發現了計時問題。 如果我添加alert()
會導致延遲,從而使代碼正確運行。 當我刪除alert()
它再次失敗。 為了簡潔起見,我試圖簡化我的代碼,而不是粘貼完整的 1,500 行文件。
function calculateSomething(podValue){
return podValue + 1;
}
function doPodStuff(value){
return value;
}
function getDimensions(calculation){
return calculation * 2;
}
function pod(value, count){
var podStuff;
var calculation;
var myDimensions;
podStuff = doPodStuff(value);
calculation = calculateSomething(podStuff):
myDimensions = getDimensions(calculation);
/* this is where my timing is off and podStuff and calculation get out of sync,
I determined this after placing an alert(myDimensions) right here If I add the alert(),
the delay makes it work, when I remove alert() it fails again.
*/
return myDimensions;
}
function row(value, count){
var myPod;
for(var i = 0; i < count; i++){
myPod = pod(value, 2);
}
return myPod;
}
row('foobar', 4);
我的目標是讓pod()
函數在for()
循環中運行,但不會亂序。 如何同步或確保時間正確?
您可以嘗試鏈接您的功能:
function pod(value, count) {
return getDimensions(calculateSomething(doPodStuff(value))) }
在任何情況下,您都可以使用$.sleep(1000)
而不是alert("...")
來減慢腳本速度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.