我建立了类似于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("");
}
};
简单,
尝试:
您可以作弊并设置全局变量。