如何使用 setTimeout / .delay() 等待字符之间的输入
我正在创建一个简单的列表框过滤器,它接受用户输入并通过 javascript/jquery 在列表框中返回匹配结果(列表框中大约有 5000 多个项目)。这是代码片段:
var Listbox1 = $('#Listbox1');
var commands = document.getElementById('DatabaseCommandsHidden'); //using js for speed
$('#CommandsFilter').bind('keyup', function() {
Listbox1.children().remove();
for (var i = 0; i < commands.options.length; i++) {
if (commands.options[i].text.toLowerCase().match($(this).val().toLowerCase())) {
Listbox1.append($('<option></option>').val(i).html(commands.options[i].text));
}
}
});
这工作得很好,但是当输入第一个/第二个字符时,由于项目太多,速度会有所减慢。
我认为我可以使用的解决方案是向文本框添加延迟,以防止调用“keyup”事件,直到用户停止键入。问题是,我不确定如何做到这一点,或者这是否是一个好主意。
非常感谢任何建议/帮助。
I am creating a simple listbox filter that takes the user input and returns the matching results in a listbox via javascript/jquery (roughly 5000+ items in listbox). Here is the code snippet:
var Listbox1 = $('#Listbox1');
var commands = document.getElementById('DatabaseCommandsHidden'); //using js for speed
$('#CommandsFilter').bind('keyup', function() {
Listbox1.children().remove();
for (var i = 0; i < commands.options.length; i++) {
if (commands.options[i].text.toLowerCase().match($(this).val().toLowerCase())) {
Listbox1.append($('<option></option>').val(i).html(commands.options[i].text));
}
}
});
This works pretty well, but slows down somewhat when the 1st/2nd char's are being typed since there are so many items.
I thought a solution I could use would be to add a delay to the textbox that prevents the 'keyup' event from being called until the user stops typing. The problem is, I'm not sure how to do that, or if its even a good idea or not.
Any suggestions/help is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以执行如下延迟:
这会在您键入的元素上存储超时,如果两次按键之间经过 500 毫秒(根据需要调整),则会执行搜索。此外,这还会将文档片段中的元素附加到 DOM 中(仍然保留编码等)。根据项目的数量,这也可能会带来不错的性能提升。
You can do a delay like this:
This stores a timeout on the element you're typing in, if 500ms (adjust as needed) passes between keystrokes, a search executes. Also this appends the elements in a document fragment then into the DOM (still preserving encoding, etc). Depending on the number of items, this may be a decent performance boost as well.
如果命令下拉列表没有改变,我建议执行以下操作(注意我已经放弃了 jQuery,以获得更好的性能和兼容性)。有几项改进:
match
替换为indexOf
快速测试表明,对于包含短字符串的 5000 个选项的下拉列表,它比大多数浏览器中的 jQuery 等效项快 10 到 30 倍。
代码:
If the commands drop-down isn't changing, I'd suggest the following (note I've dropped jQuery for better performance and compatibility). There are several improvements:
match
replaced withindexOf
A quick test suggests that for a drop-down with 5000 options containing short strings, it's between 10 and 30 times faster than the jQuery equivalent in most browsers.
Code: