Speeding up jQuery AutoComplete (Unavoidably long lists)

别说谁变了你拦得住时间么 提交于 2019-12-04 06:58:54
$(document).ready(function() {

    // once page loads, make AJAX request to get your autocomplete list and apply to HTML
    $.ajax({ url: '/path-to-get-tags-as-json.php',
        type: "GET",
        contentType: "application/json",
        success: function(tags) {
            $( "#tags" ).autocomplete({
                source: tags
            });
        }
    });
});

Place the URL to your PHP file at generates the autocomplete list in the above AJAX placeholder url parameter. In your PHP code, modify the list generation so that it returns a JSON array of values like so:

[ "first" , "second" , "anotherEntry" , "in" , "the" , "array" ]    

This will definitely not speed up the process server side, but it will protect your users from some of the delays in applying the autocomplete list. This largely assumes that the user doesn't instantly go perform an action that requires autocomplete, you can still load the page and allow the user to perform other actions. The loading of the autocomplete list should for the most part appear silent and seamless.

This is great for load times that are less than a few to several seconds, but if it's taking you longer than that then your users may still run into usability trouble.

If there are still server-side delays, consider using some timing statements to try and determine where the bottleneck is.

Since you can't lookup anything until the user types at least 1 character, you can create 26 different lists. Each autocomplete list only contains the items that begin with that letter. Your lists will be significantly smaller and faster to load.

To make it even faster, create more lists. You probably only need the first 30-40 items to display. If the item isn't in the shortened list, the user will very likely type another letter. You can then divide your lists up into 26*26 unique lists. Each list containing only items that begin with the first 2 letters.

You can divided your items up into as many lists as needed. We do this on a site I manage where we have over 500K items available in our typeahead.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!