functionasynchronousgoogle-apps-scriptinfinite-loopsimultaneous

Is there a way to run two functions at the same time(simultaneously/asynchrounously) with one function as an infinite loop?


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...
}

Solution

  • 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):

    Workaround#1: Use Different projects

    Workaround#2: Simple and Installable trigger1

    Workaround#3: Web apps: Call from client

    Workaround#4: Web apps: UrlFetchApp#fetchAll2

    Workaround#5: onEdit/onChange

    Workaround#6: Sheets API/onChange