input标签作为搜索框时延时查询
keyup事件,会在我们点击一下键盘,就会触发一下。而在真实开发过程中,我们需要通过ajax请求数据。如果点击一下,请求一下。就会频繁发送请求,不建议这样做。
所以我们需要等搜索内容输入完以后,发送一次请求。
var lastInput = null; // 上一次输入的延迟 $('输入框input标签').on('keyup', function(event) { clearTimeout(lastInput); lastInput = setTimeout(() => { // 在你停止输入250ms后,发送请求 $.ajax({ type: 'post', url: '', data: { selectParam: $(this).val() }, dataType: "json", success: function (res) { }, error: function () { layer.close(loading); } }) }, 250) });