13赞
127
当前位置:  开发笔记 > 编程语言 > 正文

使用Ajax和keypress优化搜索-OptimisedsearchusingAjaxandkeypress

IhavethefollowingcodethatIwanttousetosearchadatabaseasauseristypingintoatextbox

I have the following code that I want to use to search a database as a user is typing into a textbox. The code below works fine but it seems a little inefficient, as if a user is typing really fast. I am potentially doing many more searches than necessary. So if a user is typing in "sailing", I am searching on "sail", "saili", "sailin", and "sailing".

我有以下代码,当用户在文本框中键入时,我想用它来搜索数据库。下面的代码工作正常,但似乎有点低效,就好像用户打字速度非常快。我可能会做更多不必要的搜索。因此,如果用户输入“航行”,我正在搜索“帆”,“赛丽”,“赛林”和“帆船”。

I wanted to see if there was a way to detect any particular time between keypresses so only search if user stops typing for 500 milliseconds or something like this.

我想看看有没有办法检测按键之间的任何特定时间,所以只搜索用户是否停止输入500毫秒或类似的东西。

Is there a best practice for something like this?

这样的事情有最好的做法吗?

$('#searchString').keypress(function(e) {

    if (e.keyCode == 13) {

        var url = '/Tracker/Search/' + $("#searchString").val();
        $.get(url, function(data) {
            $('div#results').html(data);
            $('#results').show();
        });
    }
    else {

        var existingString = $("#searchString").val();
        if (existingString.length > 2) {
            var url = '/Tracker/Search/' + existingString;
            $.get(url, function(data) {
                $('div#results').html(data);
                $('#results').show();
            });
        }
    }

3 个解决方案

#1


78  

You can do something like this:

你可以这样做:

$('#searchString').keyup(function(e) {
    clearTimeout($.data(this, 'timer'));
    if (e.keyCode == 13)
      search(true);
    else
      $(this).data('timer', setTimeout(search, 500));
});
function search(force) {
    var existingString = $("#searchString").val();
    if (!force && existingString.length <3) return; //wasn't enter, not > 2 char
    $.get('/Tracker/Search/' + existingString, function(data) {
        $('div#results').html(data);
        $('#results').show();
    });
}

What this does is perform a search (on keyup, better than keypress for these situations) after 500ms by storing a timer on the #searchString element's .data() collection. Every keyup it clears that timer, and if the key was enter, searches immediately, if it wasn't sets a another 500ms timeout before auto-searching.

这样做是通过在#searchString元素的.data()集合上存储计时器,在500ms之后执行搜索(在keyup上,比这些情况下的keypress更好)。每个键盘清除该计时器,如果键输入,立即搜索,如果它没有在自动搜索之前设置另一个500ms超时。

#2


3  

See this older question:

看到这个老问题:

How do I make my live jQuery search wait a second before performing the search?

在执行搜索之前,如何使我的实时jQuery搜索等待一秒钟?

#3


1  

What I would do is each key press use a setTimeout function with the desired delay. So that function will fire after that timeout. Each key press then delete the timer and set a new one, with clearTimeout();

我要做的是每次按键使用具有所需延迟的setTimeout函数。因此该函数将在超时后触发。每按一次键然后删除定时器并设置一个新定时器,使用clearTimeout();

See here for some examples, scrolling past all the adverts.

在这里查看一些示例,滚动浏览所有广告。

http://www.elated.com/articles/Javascript-timers-with-settimeout-and-setinterval/

http://www.elated.com/articles/Javascript-timers-with-settimeout-and-setinterval/


推荐阅读
author-avatar
瞄瞄摩卡李流
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有