Javascript keystroke trapping when characters entered very rapidly
We are writing an ExtJS applicat开发者_如何学Goion that relies in large measure on trapping a users keystrokes. However I believe I have discovered a "timing issue" in that, when the keys are pressed too rapidly, unexpected behavior manifests, specifically in our case, duplicate entries get added to an array being buffered in memory.
I have an idea for a solution but am seeking input as to pitfalls to my idea, and other possible solutions. In essence my idea would be, instead of handling each keypress directly, adding the keypress to a stack, and then invoking a function/method that processes the stack. That function will always pop one item off the stack for starters. But when it is done doing so, it will check as to whether the stack has since been appended, and if so, process that entry (or entries).
A stack would give you last-in-first-out (LIFO) where you would more than likely want to utilize a queue for first-in-first-out (FIFO) as you would want to be dequeuing key characters that first came on the stack. So in theory you would want to use enqueue/dequeue as opposed to push/pop to process keys in the correct order they came in.
精彩评论