Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
590 views
in Technique[技术] by (71.8m points)

asynchronous - 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...
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

  • Create a new project: In the editor(Legacy/Old editor only)>File>New>Project
  • First project's onOpen() will run infLoop()
  • Second project's onOpen() will run runScript()
  • Both functions will run simultaneously on open.

Workaround#2: Simple and Installable trigger1

  • Create a installable trigger for runScript()
  • Simple trigger onOpen() will run infLoop()
  • Both functions will run simultaneously on open.
  • You could use two installable triggers instead of simple and installable trigger.

Workaround#3: Web apps: Call from client

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

Workaround#4: Web apps: UrlFetchApp#fetchAll2

  • UrlFetchApp#fetchAll runs asynchronously
  • Once a web app is published, the published url can be used with query parameters. If a function name is sent as a parameter and doGet() executes the function, .fetchAll can be used to multiple functions asynchronously.

Workaround#5: onEdit/onChange

  • If a edit is made, both functions(onEdit/onChange) run simultaneously.

Workaround#6: Sheets API/onChange

  • If a add-on/script makes a change through sheets api, onChange may get triggered. If triggered, every change made through sheets api causes onChange to run asynchronously.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...