Javascript 缓存Jquery UI自动完成组合框

Javascript 缓存Jquery UI自动完成组合框,javascript,jquery,jquery-ui,caching,autocomplete,Javascript,Jquery,Jquery Ui,Caching,Autocomplete,我在现有项目上使用jQueryUI自动完成。嗯,性能很慢,尤其是在执行 input.autocomplete("search", ""); 我的解决方案是缓存信息,所以即使它的狗慢了,它也只会发生一次。我想我错过了一个非常简单的Javascript错误,我真的非常感谢您的帮助 这是密码 input.autocomplete( { delay: 0, minLength: 0, source: functio

我在现有项目上使用jQueryUI自动完成。嗯,性能很慢,尤其是在执行

input.autocomplete("search", "");
我的解决方案是缓存信息,所以即使它的狗慢了,它也只会发生一次。我想我错过了一个非常简单的Javascript错误,我真的非常感谢您的帮助

这是密码

input.autocomplete(
        {
            delay: 0,
            minLength: 0,
            source: function (request, response)
            {
                if (request.term in cache)
                {
                    response(cache[request.term]);
                    return;
                }
                // The source of the auto-complete is a function that returns all the select element's child option elements.
                var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
                response(select.children("option").map(function ()
                {
                    var text = $(this).text();
                    if (this.value && (!request.term || matcher.test(text)))
                    {
                        cache[request.term] = text;
                        return { label: text, value: text, option: this };
                    }
                }));
            },

            select: function (event, ui)
            {
                // On the select event, trigger the "selected" event with the selected option. Also update the select element
                // so it's selected option is the same.
                ui.item.option.selected = true;
                self._trigger("selected", event,
                {
                    item: ui.item.option
                });
            },

            change: function (event, ui)
            {
                // On the change event, reset to the last selection since it didn't match anything.
                if (!ui.item)
                {
                    $(this).val(select.children("option[selected]").text());
                    return false;
                }
            }
        });

        // Add a combo-box button on the right side of the input box. It is the same height as the adjacent input element.
        var autocompleteButton = $("<button type='button' />");
        autocompleteButton.attr("tabIndex", -1)
                          .attr("title", "Show All Items")
                          .addClass("ComboboxButton")
                          .insertAfter(input)
                          .height(input.outerHeight())
                          .append($("<span />"))
        autocompleteButton.click(function ()
        {
            // If the menu is already open, close it.
            if (input.autocomplete("widget").is(":visible"))
            {
                input.autocomplete("close");
                return;
            }

            // Pass an empty string as value to search for -- this will display all results.
            input.autocomplete("search", "");
            input.focus();
        });
input.autocomplete(
{
延迟:0,
最小长度:0,
来源:功能(请求、响应)
{
if(缓存中的request.term)
{
响应(缓存[request.term]);
回来
}
//自动完成的源函数是一个返回select元素的所有子选项元素的函数。
var matcher=newregexp($.ui.autocomplete.escapeRegex(request.term),“i”);
响应(选择.children(“选项”).map(函数()
{
var text=$(this.text();
if(this.value&(!request.term | | matcher.test(text)))
{
cache[request.term]=文本;
返回{label:text,value:text,option:this};
}
}));
},
选择:功能(事件、用户界面)
{
//在select事件上,使用select选项触发“select”事件。同时更新select元素
//所以它的选择选项是相同的。
ui.item.option.selected=true;
自触发(“选定”,事件,
{
项目:ui.item.option
});
},
更改:功能(事件、用户界面)
{
//在更改事件中,重置为最后一个选择,因为它与任何内容都不匹配。
如果(!ui.item)
{
$(this.val(select.children(“option[selected]”)和.text());
返回false;
}
}
});
//在输入框的右侧添加一个组合框按钮。它与相邻输入元素的高度相同。
变量自动完成按钮=$(“”);
autocompleteButton.attr(“tabIndex”,-1)
.attr(“标题”、“显示所有项目”)
.addClass(“ComboboxButton”)
.insertAfter(输入)
.height(输入.outerHeight())
.append($(“”)
自动完成按钮。单击(函数()
{
//如果菜单已打开,请将其关闭。
如果(input.autocomplete(“小部件”)为(“:可见”))
{
输入。自动完成(“关闭”);
回来
}
//传递一个空字符串作为要搜索的值--这将显示所有结果。
input.autocomplete(“搜索”和“);
input.focus();
});
除了我微弱的缓存尝试之外,几乎所有的代码都是默认的jQueryUIComboBox示例代码。它返回下拉列表中的每个字符

例如,如果返回的解决方案集是rabble,而下一个是foobar,“缓存数据”将如下所示 F o o B A. R 各自为政

我需要它 暴民 福巴

这将是非常好的,如果这也适用于一个空字符串,因为这是我最累人的电话


感谢您的帮助

jQuery默认自动完成缓存。您可能需要做的是在服务器端进行一些分页或缓存,以限制性能紧张。您使用的服务器端技术是什么?

如果您通过设置适当的HTTP头来缓存webservice结果,将会更加容易。这样,浏览器将为您解决所有令人讨厌的缓存失效问题

在C#中,您可以在一年后设置Expires标头,如下所示:

Response.Cache.SetExpires(DateTime.Now.AddYears(1));
HTTP缓存的最佳参考是MarkNottingham的。任何我没有回答的问题都在那份文件里


dotnetperls.com是一个更具C#特色的资源。

我们这里是一个.net商店,所以服务器端是C#有几种方法可以解决这个问题。我发现的最好的方法就是缓存整个结果集,并在一段时间后使其过期。它的每个后续查询都与缓存相冲突。