您好我无法弄清楚为什么当直接传递给keyup事件时debounce函数按预期工作; 但如果我将它包装在匿名函数中,它就不起作用.
我有问题:http : //jsfiddle.net/6hg95/1/
编辑:添加了我尝试过的所有东西.
HTML
<input id='anonFunction'/>
<input id='noReturnAnonFunction'/>
<input id='exeDebouncedFunc'/>
<input id='function'/>
<div id='output'></div>
Run Code Online (Sandbox Code Playgroud)
JAVASCRIPT
$(document).ready(function(){
$('#anonFunction').on('keyup', function () {
return _.debounce(debounceIt, 500, false); //Why does this differ from #function
});
$('#noReturnAnonFunction').on('keyup', function () {
_.debounce(debounceIt, 500, false); //Not being executed
});
$('#exeDebouncedFunc').on('keyup', function () {
_.debounce(debounceIt, 500, false)(); //Executing the debounced function results in wrong behaviour
});
$('#function').on('keyup', _.debounce(debounceIt, 500, false)); //This is working.
});
function debounceIt(){
$('#output').append('debounced');
}
Run Code Online (Sandbox Code Playgroud)
anonFunction并且noReturnAnonFunction不会启动去抖功能; 但是最后一次function …
如何debounce在async功能上使用?我的vue-app中有一个方法,该方法可从API检索数据,该方法连续调用该API,而我想避免这种情况。
这是我的方法:
methods: {
async getAlbums () {
const response = await AlbumService.fetchAlbums()
this.albums = response.data.albums
}
}
Run Code Online (Sandbox Code Playgroud)
我以前已经安装了,lodash那么如何实现呢?