gpt4 book ai didi

javascript - 是否可能/如何像 Google 插件一样设置 jQuery 自动完成的延迟?

转载 作者:行者123 更新时间:2023-11-27 23:10:03 24 4
gpt4 key购买 nike

我正在使用 jQuery 插件 Autocomplete like Google而且效果很好。但每次按键都会触发 AJAX 请求。这意味着,字符串 washington 将引发十个 (!) 个请求。那么,可以通过使用选项minLength来减少请求的数量。 。但例如使用 minLength:3 我们仍然发送七个请求(从第四个字符开始)。

$(function() {
$("#bar").autocomplete({
minLength: 3,
limit: 5,
source : [
function(query, add) {
fooNumber = $('#foo-number').val();
$.getJSON("/my/ajax/controller/bar?data[number]=" + query + "&data[foo_name]=" + fooNumber, function(response) {
add(response);
})
}],
});
});

当用户自例如300 ms之后没有输入内容时,如何防止即时请求服务器并使脚本仅发送新请求?

最佳答案

这可能有用。 (未测试)

//global variable
var keyPressed = new Date();
// Call where you're watching for pressed keys.
var updateKeyPressed = function(){
keyPressed = new Date();
}

$(function() {
$("#bar").autocomplete({
minLength: 3,
limit: 5,
appendMethod:'replace',
closeOnBlur: true,
source : [
function(query, add) {
var currentDate = new Date();
setInterval(function(){
currentDate = new Date();
if (currentDate.getTime() > keyPressed.getTime()) {
orderApplicationNumber = $('#foo-number').val();
$.getJSON("/my/ajax/controller/bar?data[number]=" + query + "&data[foo_name]=" + fooNumber, function(response) {
add(response);
});
}
}, 300);
}
],
});
});

关于javascript - 是否可能/如何像 Google 插件一样设置 jQuery 自动完成的延迟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36265912/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com