有没有一种方法可以同时(同时/异步)运行两个函数,而一个函数是无限循环? [英] Is there a way to run two functions at the same time(simultaneously/asynchrounously) with one function as an infinite loop?

查看:197
本文介绍了有没有一种方法可以同时(同时/异步)运行两个函数,而一个函数是无限循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想同时运行两个函数,但是我不能只让它们分别运行,因为一个函数包含无限循环 while(true).JavaScript的问题在于,如果您要在哪里运行两个函数,它将在运行下一个函数之前完成该函数的运行.因此,如果我使用 while(true)循环运行一个函数,它将永远不会移至下一个函数.
如果您仍然不明白,请参阅以下代码:

  function onOpen(){//Google Apps脚本触发器infLoop()//如何同时运行这两个函数?runScript()}函数infLoop(){//inf循环.而(真实){Utilities.sleep(100)DocumentApp.getActiveDocument().setname(请勿更改此名称")}}函数runScript(){//代码...} 

解决方案

Google Apps脚本同步执行.在大多数情况下,无法同时进行/并行处理.根据您的脚本,似乎您希望两个函数在onOpen上同时运行.可能的解决方法(有些未经测试):

解决方法#1:使用不同的项目

  • 创建新项目:在编辑器中(仅旧版/旧版编辑器)>文件>新建>项目
  • 第一个项目的 onOpen()将运行 infLoop()
  • 第二个项目的 onOpen()将运行 runScript()
  • 这两个功能将同时在打开时运行.

解决方法2:简单且可安装的触发器 1

  • runScript()
  • 创建可安装的触发器
  • 简单触发器 onOpen()将运行 infLoop()
  • 这两个功能将同时在打开时运行.
  • 您可以使用两个可安装的触发器,而不是简单且可安装的触发器.

解决方法#3: Web应用:来自客户端的呼叫

  • 如果打开了侧边栏或通过Web应用程序打开了工作表,则可以通过 google.script.run (异步运行)重复调用服务器功能>

  • 在此处可以运行一个功能6分钟(当前运行时).但是通过反复调用服务器功能,您可以长时间运行该功能(最多90分钟/天=当前触发器运行时配额/天)

解决方法4:网络应用:UrlFetchApp#fetchAll 2

  • UrlFetchApp#fetchAll异步运行
  • 发布Web应用程序后,发布的URL可以与查询参数一起使用.如果将函数名称作为参数发送,并且 doGet()执行该函数,则 .fetchAll 可以异步用于多个函数.

解决方法#5: onEdit/onChange

  • 如果进行编辑,则两个功能(onEdit/onChange)将同时运行.

解决方法#6: Sheets API/onChange

  • 如果加载项/脚本通过工作表api进行更改,则可能触发 onChange .如果被触发,则通过sheets api进行的每个更改都会导致onChange异步运行.

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

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.

这篇关于有没有一种方法可以同时(同时/异步)运行两个函数,而一个函数是无限循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆