javascript - 如何在搜索过滤器上添加去抖动(延迟)时间?Javascript
问题描述
所有代码示例都在这里:
我需要为这个搜索添加去抖动时间(在每个 keyup 延迟上)?就像keyup一样。每延迟 0.5 秒。
var searchRequest = null;
$(function () {
var minlength = 3;
$("#sample_search").keyup(function () {
var that = this,
value = $(this).val();
console.log('value', value)
if (value.length >= minlength ) {
if (searchRequest != null)
searchRequest.abort();
searchRequest = $.ajax({
type: "GET",
url: "https://jsonplaceholder.typicode.com/comments",
data: {
'search_keyword' : value
},
dataType: "text",
success: function(msg){
//we need to check if the value is the same
if (value==$(that).val()) {
console.log('value real' , msg)
//Receiving the result of search here
}
}
});
}
});
});
解决方案
看看下面的代码。
$("#sample_search").keyup(function() {
// If the user kept typing. Delete the previous timeout.
clearTimeout(delayTime);
// call 'ajaxCall' function after 500ms
var delayTime = setTimeout(ajaxCall, 500);
});
推荐阅读
- modulo - 在 CGAL 上使用 mod 进行双打
- javascript - 在对象数组的嵌套对象数组中查找字符串
- swift - ReplayKit:RPScreenRecorder.shared().startCapture() 不工作
- javascript - 布尔值不变
- database - 如何使用 oracle 即时客户端创建 ODBC 数据源?
- javascript - 将 Blob 转换为文件并在浏览器上预览(不下载)
- python - 使用 Flask 在 URL 中使用符号
- mysql - 如何选择零结果mysql?
- cmake - 默认使用静态库构建 llvm/clang
- selenium - 自动 gmail 登录 [在 oAuth 期间] 被用户验证阻止