在自动完成上添加jQuery延迟 [英] Adding a jQuery delay on autocomplete
本文介绍了在自动完成上添加jQuery延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试为应用程序创建jQuery自动完成:
I'm trying to create a jQuery autocomplete for an application:
$("#search-input").on('keyup', function() {
search = $(this).val();
autocomplete_div = $(".autocomplete")
$.get('/ajax/search/', {'search': search,}, function(response){
autocomplete_div.html(response)
});
});
我需要添加到上面的代码中才能添加400毫秒的延迟?
What would I need to add to the above code to add a 400ms delay?
推荐答案
使用
setTimeout(function() {
// your code here
}, 400);
setTimeout 是浏览器的窗口
对象提供的方法。
setTimeout is a method provided by the browser's window
object.
如果已使用 clearTimeout 设置,则取消计时器的更完整示例将是:
A more complete example that cancels a timer if already set using clearTimeout would be:
var myTimer = 0;
$("#search-input").on('keydown', function() {
search = $(this).val();
// cancel any previously-set timer
if (myTimer) {
clearTimeout(myTimer);
}
myTimer = setTimeout(function() {
autocomplete_div = $(".autocomplete")
$.get('/ajax/search/', {'search': search,}, function(response){
autocomplete_div.html(response)
});
}, 400);
});
另请注意在上使用而不是已弃用
直播
。
这篇关于在自动完成上添加jQuery延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文