删除'onKeyUp'搜索

Removing multiple request for 'onKeyUp' search

本文关键字:搜索 onKeyUp 删除      更新时间:2023-09-26

这是中止使用'onkeyup'搜索时创建的多个请求的正确方法吗?

(注意:对于多个请求,我指的是为字符串的每个单独字符创建的顺序请求。例如:"Hello"创建"H","He",……'Hello' - 5个不同的请求)

var insideReq = false;                      /* Initialise to false */
$('#search').keyup( function() {
    var SearchString = $('#search').val();
    if((SearchString.length) >= 3) {
        if(insideReq == true )              /* Check if somebody is inside */
            ajaxReq.abort();                /* If yes, then throw her out */
        insideReq = true;                   /* Inform I am inside */
        var ajaxReq = $.get('search.exec.php', {q: SearchString}, function(ajaxContent) {
            $('#container-list').html("<img src='"img''busy.gif'" class='"busy-indicator'"/>");
            $('#container-list').fadeOut(5);
            $('#container-list').html(ajaxContent);
            $('#container-list').fadeIn(1500);
            insideReq = false;          /* Work done; I am going */
        });
    }
});

看起来不错。一些代码优化技术可以让你的代码执行得更快。

var insideReq = false;                      /* Initialise to false */
$('#search').keyup( function() {
    var SearchString = $('#search').val();
    if((SearchString.length) >= 3) {
        if(insideReq == true )              /* Check if somebody is inside */
            ajaxReq.abort();                /* If yes, then throw her out */
        insideReq = true;                   /* Inform I am inside */
        var ajaxReq = $.get('search.exec.php', {q: SearchString}, function(ajaxContent) {
            $('#container-list')
            .html("<img src='"img''busy.gif'" class='"busy-indicator'"/>");
            .fadeOut(5);
            .html(ajaxContent);
            .fadeIn(1500);
            insideReq = false;          /* Work done; I am going */
        });
    }
});

您希望在某人停止键入时触发此命令。创建一个定时器,比如300ms。清除并重置每次击键时的计时器。当计时器超时时,触发AJAX请求。