我有一个搜索字段,它接受用户输入并使用去抖事件侦听器发出 ajax 请求。
html:
<input id="search" type="text"></input>
javascript:
function makeRequest() {
// make ajax request, do things with the information
}
$('#search').on('keypress', _.debounce(makeRequest, 200));
我需要事件监听器not在向上和向下箭头上使用 debounced ajax 函数,即event.keyCode === 38
or event.keyCode === 40
有没有办法应用来自的建议this https://stackoverflow.com/questions/11270275/javascript-how-do-i-tweak-my-debounce-function-to-take-an-if-conditional对我的问题提出问题?
确保保存结果并仅调用该函数,在每次按键时创建该函数会产生不必要的开销。
var debounced = _.debounce(makeRequest, 200);
$('#search').on('keypress', function(event) {
// just exit if it's an up or down arrow
if (event.which === 38 || event.which === 40) return;
debounced();
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)