有什么办法可以保证执行顺序吗?或者有什么方法取消当前正在执行的脚本? [英] Is there any way to guarantee order of execution? Or is there any way to cancel currently executing scripts?

查看:70
本文介绍了有什么办法可以保证执行顺序吗?或者有什么方法取消当前正在执行的脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文本框上有一个类似自动完成的功能。

  textBox.addKeyUpHandler(textBoxLookupHandler)



会发生什么



如果用户输入的速度比较快,比如 a b ,看起来会发生以下情况。


  1. a 被调用。

  2. 调用 ab 的处理程序。 b
  3. ab 返回的结果较少。因此,它会在 a 的处理程序之前返回

  4. <$ c $的处理程序然后返回许多结果。


  5. 因此,最终用户输入 ab ,但是它们被显示为 a 的结果,因为 a 覆盖了 ab 的结果。

    可能的解决方案



    如果我可以写一些客户端脚本,我知道我会如何处理这个问题。但是,由于我使用UiApp,所以我无法做到这一点。


    1. 保证GAS执行顺序(我确信这不是'可能/是一个荒谬的请求)

    2. 在GAS中有一些方法可以取消所有其他当前正在运行的脚本。


    解决方案

    GAS有一个锁定服务,以保证您的执行顺序。请参阅Google Apps Developer Blog并发和Google Apps脚本




    $ b $您的处理程序看起来应该是这样的:

      function textBoxLookupHandler(e){
    var lock = LockService.getPrivateLock(); //锁定这个用户
    lock.waitLock(15000); //在例外之前等待最多15秒。

    //做任何你曾经做过的事情...

    lock.releaseLock();
    返回应用程序;
    }


    I have an auto-complete-like feature on a text box.

    textBox.addKeyUpHandler(textBoxLookupHandler)
    

    What happens

    If the user is typing relatively quickly, say a b, it seems the following happens.

    1. The handler for a is invoked.
    2. The handler for ab is invoked.
    3. ab returns fewer results. Because of this, it returns before the handler for a.
    4. The handler for a then returns many results.

    So in the end, the user typed ab, but they are being shown the results for a because the results for a overwrote the results for ab.

    Possible solutions

    If I could write some client-side scripting, I know how I would handle this issue. But since I'm using UiApp, I can't do that.

    1. Guarantee order of execution of GAS (I'm sure this isn't possible/is a ridiculous request)
    2. Have some method in GAS to cancel all other currently running scripts.

    解决方案

    GAS has a Lock Service that will guarantee order of execution for you. See the Google Apps Developer Blog "Concurrency and Google Apps Script" entry.

    Your handler should look something like this:

    function textBoxLookupHandler(e) {
      var lock = LockService.getPrivateLock(); // Lock for just this user
      lock.waitLock(15000);  // wait max 15 seconds before exception.
    
      // Do whatever you used to do...
    
      lock.releaseLock();
      return app;
    }
    

    这篇关于有什么办法可以保证执行顺序吗?或者有什么方法取消当前正在执行的脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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