listviewbeforefilter / asynchronous AJAX request / update

listviewbeforefilter / asynchronous AJAX request / update

I use the following code for a listviewbeforeupdate with external data.

Some changes from the examples on this site:
- no page ID is identified as the javascript is only inserted dynamically with PHP subject to the page loaded).
- no reload if >3 letters

The latter adjustment should be a performance booster as the data-filter is quicker than multiple Ajax requests (at least in my case)

Everything works fine with a synchronous Ajax request. However with an asynchronous Ajax request (which I prefer as it is more user friendly) it may happen that the user has typed data after the Ajax request is completed. The data-filter is than only updated if more data is entered. 

What I would like to do is file a "data-filter update" request after the " $ul.trigger( "updatelayout");"

Live example: http://jetjesstrijk.x10.mx/btw/iphone2.php?pagina=zaaki example seach query C-77

  1. <script>
  2. $( document ).on( "pageinit", function() {
  3. $( "#autocomplete" ).on( "listviewbeforefilter", function ( e, data ) {
  4. var $ul = $( this ),
  5. $input = $( data.input ),
  6. value = $input.val(),
  7. html = "";
  8. if (value.length < 4 ) {
  9.         $ul.html( "" ); /*no longer use AJAX if > 3 letters */
  10.         }
  11. if ( value && value.length > 2 && value.length < 4 ) {
  12. $ul.html( "<li><div class='ui-loader'><span class='ui-icon ui-icon-loading'></span></div></li>" );
  13. $ul.listview( "refresh" );
  14.           var xmlhttp;
  15.           xmlhttp=new XMLHttpRequest();

  16.           xmlhttp.onreadystatechange=function()
  17.             {
  18.             if (xmlhttp.readyState==4)
  19.               {
  20.               html = xmlhttp.responseText;
  21.               $ul.html( html );
  22.   $ul.listview( "refresh" );
  23.   $ul.trigger( "updatelayout");
  24.               }
  25.             }
  26.           xmlhttp.open("GET","ajax.php?q="+value,true);
  27.           xmlhttp.send();
  28. }
  29. });
  30. });
  31. </script>