Javascript:如何使setTimeout不延迟首次执行?

问题描述 投票:0回答:3

我建立了类似于Google搜索的即时搜索功能。 setTimeout函数用于消除/限制搜索的跳动。但是,我不希望第一次执行searchq()函数会有延迟。如何修改代码以实现此目的?

var timeout = null;

function doDelayedSearch(txt) {

          if (timeout) {  
            clearTimeout(timeout);
          }

            timeout = setTimeout(function() {
                searchq(txt); //this is the search function (posting and fetching result)
                }, 1000);
}

function searchq(txt){
        // get the value
            // txt = $("input").val();
            // post the value
            if(txt){
                $.post("search.php", {searchVal: txt}, function(result){
                    $("#search_output").html(result+"<br><a class='anchor_tag_s' href='createobject.php?object="+txt+"'><div id='notfound'>Not found above? Create Here.</div><a>");
                });
            }
            else{
                $("#search_output").html("");
            }
        };
javascript jquery html search
3个回答
0
投票

简单,


0
投票

尝试:


0
投票

您可以作弊并设置全局变量。

© www.soinside.com 2019 - 2024. All rights reserved.