I have two functions that I want to run at the same time but I can't just let them run separately as one function contains an infinite loop while(true)
. And the problem with JavaScript is that if you where to run two functions, it will finish running the function before running the next one; so if I run a function with a while(true)
loop, it will never move onto the next function.
If you still don't understand, here is my code:
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...
}
Google apps script executes synchronously. For the most part, simultaneous/paralell processing is not possible. Based on your script, it seems you want two functions to run simultaneously onOpen. Possible workarounds(Some not tested):
onOpen()
will run infLoop()
onOpen()
will run runScript()
runScript()
onOpen()
will run infLoop()
If there is a sidebar open or if a sheet is opened from web app, it is possible to call server functions repeatedly through google.script.run
(which run asynchrously)
Here It is possible to run a function for 6 minutes(current runtime). But by repeatedly calling the server function, you can run the function for a long time(upto 90minutes/day = current trigger runtime quota/day)
doGet()
executes the function, .fetchAll
can be used to multiple functions asynchronously.onChange
may get triggered. If triggered, every change made through sheets api causes onChange to run asynchronously.