I have an Ajax jquery function ($.post) that retrieves json data. One of the ways it gets executed is by keydown ($(‘#lkj’).keyup(function(event)) with a 250 ms delay. One problem however is that the data retrieved is not always up to date. Especially when the user has a high ping towards the server. When i for example type “test”, the jquery .post script will get (depending on my typing speed) executed between two to four times:
- t
- te
- tes
- test
Sometimes the instance containing “te” is slow and is last the complete and therefore overwrites the output written by the instance containing “test”. How do I prevent that?
One technique is using a sequence number with your request, and processing the output only if it matches the latest request sequence number.
For example:
When making your $.post call: