【发布时间】:2020-01-23 15:16:38
【问题描述】:
我有两个想要同时运行的函数,但我不能让它们单独运行,因为一个函数包含无限循环while(true)。而 JavaScript 的问题是,如果你在哪里运行两个函数,它会在运行下一个函数之前完成函数的运行;因此,如果我使用 while(true) 循环运行一个函数,它将永远不会移动到下一个函数。
如果你还是不明白,这是我的代码:
function onOpen(){ // Google Apps Script trigger
infLoop() //how to run both of these functions at the same time?
runScript()
}
function infLoop(){ //inf loop.
while(True){
Utilities.sleep(100)
DocumentApp.getActiveDocument()
.setname("dont change this name")
}
}
function runScript(){
//code...
}
【问题讨论】:
-
为什么?用例
-
Apps 脚本有一个最大的脚本运行时间限制developers.google.com/apps-script/guides/services/quotas,这将不允许您运行无限循环。作为一种解决方法,我建议您设置一个基于时间的触发器,允许您以您选择的时间间隔运行您的函数:developers.google.com/apps-script/guides/triggers/installable
-
例如,这个 GAS 库对您的情况有用吗? github.com/tanaikech/RunAll
-
另一个选项是gasThreader。请参阅:sites.google.com/a/mcpher.com/share/Home/excelquirks/… 和 github.com/brucemcpherson/gasThreader
标签: function asynchronous google-apps-script infinite-loop simultaneous