Rapid typing into a text field updated by AJAX can lose data

mistlethrush - March 11, 2008 - 17:09
Project:Ajax Table
Version:5.x-1.2
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:needs review
Description

I'm using this module in a data entry/display module I'm writing, so thank you very much indeed for such a neat development.

I've hit one snag so far. I have a slow cumbersome test server that I'm using. I use a text field in my table, with associated callbacks as described in the documentation, and it all works - if I type into the field, the callback gets called and my code runs to update the database row with the new text. However... if I type very quickly, so that new characters arrive before the previous ajax transaction completes, the newer characters don't make it into the database. (as an example, if I type "aaaaa" very quickly, I get "a" or "aa" in the database). If I type one final character once the spinner has gone back to a green tick, then the data is updated properly as you'd expect.

I imagine this is because the callbacks are asynchronous, and the later keystrokes that arrive while an earlier callback is running don't get registered. This might not have been noticed before on faster servers, but if my application is to work in a potentially laggy environment (e.g. over a slow VPN link) it'll be important to fix this.

My "hacky" idea for fixing this, if it's possible, was to put a delay in somehow so that it waits a couple of seconds after the last keystroke before initiating the ajax call. I don't even know if that's possible as yet though from the javascript point of view. The ideal fix is to somehow register that it's had new data during the initial ajax transaction, and for it to re-sync the data again.

Any thoughts please? :)

Regards

Paul.

#1

mistlethrush - April 9, 2008 - 14:59
Status:active» needs review

Well, it appears that the author of this module is on a long holiday or something :)

I fixed the problem myself by wrapping the calls inside onkeyup events inside a call to window.setTimeout with 1 second interval that gets reset whenever additional input is made. Hopefully this is a) not too hacky a solution, and b) a sensible way to do it. It works fine on my test and production server now, both in the search input box and in text entry fields. I hope this is useful for someone. Patch file enclosed.

Regards

Paul.

AttachmentSize
diffs.txt 2.34 KB
 
 

Drupal is a registered trademark of Dries Buytaert.