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