有什么办法可以保证执行顺序吗?或者有什么方法取消当前正在执行的脚本? [英] Is there any way to guarantee order of execution? Or is there any way to cancel currently executing scripts?
问题描述
我在文本框上有一个类似自动完成的功能。
textBox.addKeyUpHandler(textBoxLookupHandler)
会发生什么
如果用户输入的速度比较快,比如 a
b
,看起来会发生以下情况。
-
a
被调用。 - 调用
ab
的处理程序。 b -
ab
返回的结果较少。因此,它会在a
的处理程序之前返回。 - <$ c $的处理程序然后返回许多结果。
- 保证GAS执行顺序(我确信这不是'可能/是一个荒谬的请求)
- 在GAS中有一些方法可以取消所有其他当前正在运行的脚本。
- The handler for
a
is invoked. - The handler for
ab
is invoked. ab
returns fewer results. Because of this, it returns before the handler fora
.- The handler for
a
then returns many results. - Guarantee order of execution of GAS (I'm sure this isn't possible/is a ridiculous request)
- Have some method in GAS to cancel all other currently running scripts.
因此,最终用户输入 ab
,但是它们被显示为 a
的结果,因为 a
覆盖了 ab
的结果。
可能的解决方案
如果我可以写一些客户端脚本,我知道我会如何处理这个问题。但是,由于我使用UiApp,所以我无法做到这一点。
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.
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.
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屋!