So I am making a autocomplete with jquery. Ajax is used to get a heavy xml file from remote location. If I search for ex. "Two and a half men" it does about 16 querys (for every letter typed except the 3 first, look the code) with ajax.
所以我用jquery做了一个自动完成。 Ajax用于从远程位置获取繁重的xml文件。如果我搜索前。 “两个半人”它用ajax做了大约16个查询(对于除了3个之外的每个字母,查看代码)。
I want it to do the Ajax request a second after user has pressed the last key so it will do one query instead of 16.
我希望它在用户按下最后一个键后一秒钟执行Ajax请求,这样它将执行一个查询而不是16个查询。
So if user presses another key before it's been 1 second since the last keydown event, it won't do the query yet.
因此,如果用户在上次keydown事件后的1秒钟之前按下另一个键,它将不会执行查询。
<script>
$(document).ready(function(){
$('#tv_search').keydown(function(){
var search = $(this).val();
if(search.length >= 3)
{
$.ajax({
type: "GET",
url: "search.php",
data: {show : search, key : '890k4900k0ll' }
}).done(function(msg){
$('#t').html(msg);
});
}
});
});
</script>
Search for series: <input type='text' name='tv_search' id='tv_search'>
Any ideas?
4 个解决方案
#1
6
You could just use a timeout, and clear it every time a key is pressed :
您可以使用超时,并在每次按下键时清除它:
$(document).ready(function() {
$('#tv_search').on('keyup', function() {
clearTimeout($(this).data('timer'));
var search = this.value;
if (search.length >= 3) {
$(this).data('timer', setTimeout(function() {
$.ajax({
type: "GET",
url: "search.php",
data: {
show: search,
key: '890k4900k0ll'
}
}).done(function(msg) {
$('#t').html(msg);
});
}, 1000));
}
});
});
#2
3
To avoid multiple AJAX request generating at each key-press try to use setTimeout()
and clearTimeout()
methods in a way that you cancel the last timeout and start a new timeout by calling setTimeout()
after clearing it. setTimeout()
method should contain the AJAX request which executes after quarter of second (250 millis) user press a key.
为避免在每次按键时生成多个AJAX请求,请尝试以取消上次超时的方式使用setTimeout()和clearTimeout()方法,并在清除之后通过调用setTimeout()来启动新的超时。 setTimeout()方法应包含AJAX请求,该请求在四分之一秒(250毫秒)用户按下一个键后执行。
I also abort()
the request if its available.
如果请求可用,我也会中止()。
var timeOut = null,
myXHR = null;
$(document).ready(function(){
$('#tv_search').keydown(function(){
// It will clear the setTimeOut activity if valid.
if(timeOut) clearTimeout(timeOut);
var search = $(this).val();
if(search.length >= 3)
{
timeOut = setTimeout(function(){
// Cancel the last request if valid
if(myXHR) myXHR.abort();
myXHR = $.ajax({
type: "GET",
url: "search.php",
data: {show : search, key : '890k4900k0ll' }
}).done(function(msg){
$('#t').html(msg);
});
}, 250);// wait for quarter second.
}
});
});
#3
0
I have a solution that allows you to delay any method you call, avoiding repetitive calls, that would be not used at all!
我有一个解决方案,允许你延迟你调用的任何方法,避免重复调用,根本不使用!
#4
0
quit simple solution :
退出简单的解决方案:
<script>
$(document).ready(function(){
var mytimer;
$('#tv_search').keydown(function(){
clearTimeout(mytimer);
var search = $(this).val();
if(search.length >= 3)
{
mytimer = setTimeout(function(){
$.ajax({
type: "GET",
url: "search.php",
data: {show : search, key : '890k4900k0ll' }
}).done(function(msg){
$('#t').html(msg);
});
}, 1000);
}
});
});
Where 'mytimer' must be global variable, simply clear previous 'mytimer' on every event call. It will only execute latest call in interval of 1 second.
'mytimer'必须是全局变量,只需在每个事件调用中清除之前的'mytimer'。它只会在1秒的间隔内执行最新的呼叫。
#1
6
You could just use a timeout, and clear it every time a key is pressed :
您可以使用超时,并在每次按下键时清除它:
$(document).ready(function() {
$('#tv_search').on('keyup', function() {
clearTimeout($(this).data('timer'));
var search = this.value;
if (search.length >= 3) {
$(this).data('timer', setTimeout(function() {
$.ajax({
type: "GET",
url: "search.php",
data: {
show: search,
key: '890k4900k0ll'
}
}).done(function(msg) {
$('#t').html(msg);
});
}, 1000));
}
});
});
#2
3
To avoid multiple AJAX request generating at each key-press try to use setTimeout()
and clearTimeout()
methods in a way that you cancel the last timeout and start a new timeout by calling setTimeout()
after clearing it. setTimeout()
method should contain the AJAX request which executes after quarter of second (250 millis) user press a key.
为避免在每次按键时生成多个AJAX请求,请尝试以取消上次超时的方式使用setTimeout()和clearTimeout()方法,并在清除之后通过调用setTimeout()来启动新的超时。 setTimeout()方法应包含AJAX请求,该请求在四分之一秒(250毫秒)用户按下一个键后执行。
I also abort()
the request if its available.
如果请求可用,我也会中止()。
var timeOut = null,
myXHR = null;
$(document).ready(function(){
$('#tv_search').keydown(function(){
// It will clear the setTimeOut activity if valid.
if(timeOut) clearTimeout(timeOut);
var search = $(this).val();
if(search.length >= 3)
{
timeOut = setTimeout(function(){
// Cancel the last request if valid
if(myXHR) myXHR.abort();
myXHR = $.ajax({
type: "GET",
url: "search.php",
data: {show : search, key : '890k4900k0ll' }
}).done(function(msg){
$('#t').html(msg);
});
}, 250);// wait for quarter second.
}
});
});
#3
0
I have a solution that allows you to delay any method you call, avoiding repetitive calls, that would be not used at all!
我有一个解决方案,允许你延迟你调用的任何方法,避免重复调用,根本不使用!
#4
0
quit simple solution :
退出简单的解决方案:
<script>
$(document).ready(function(){
var mytimer;
$('#tv_search').keydown(function(){
clearTimeout(mytimer);
var search = $(this).val();
if(search.length >= 3)
{
mytimer = setTimeout(function(){
$.ajax({
type: "GET",
url: "search.php",
data: {show : search, key : '890k4900k0ll' }
}).done(function(msg){
$('#t').html(msg);
});
}, 1000);
}
});
});
Where 'mytimer' must be global variable, simply clear previous 'mytimer' on every event call. It will only execute latest call in interval of 1 second.
'mytimer'必须是全局变量,只需在每个事件调用中清除之前的'mytimer'。它只会在1秒的间隔内执行最新的呼叫。