Is there any way to guarantee order of execution?

2019-08-13 02:47发布

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.

1条回答
趁早两清
2楼-- · 2019-08-13 03:25

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;
}
查看更多
登录 后发表回答