当直接未绑定到按钮时,去抖function不起作用
我正在尝试使用Ben Alman的debounce
代码 。 我有以下代码,但我根本没有看到任何执行。
onChange() { var this_ = this if (this.autoRefreshOn) { Cowboy.debounce(function(e) { console.log("debounce worked"); this_.doSomethingFunction(); }, 250); } }
这个onChange()
函数是从multiselect
选框中触发的,如下所示:
当选中这些选择框时,它们会持续触发onChange()
但我看不到debounce
function的任何执行。
我在网上找到的所有例子都实现了一个与按钮按下或类似的东西绑定的去抖function。
你可以直接在你的onChange()
方法中添加一个debounce,并直接在你的模板中调用新创建的debounced方法:
component.ts
limitedOnChange = this.debounce(this.onChange, 250); onChange(event) { console.log('change detected: ', event) } debounce(fn, delay) { let timer = null; return function () { const context = this, args = arguments; clearTimeout(timer); timer = setTimeout(function () { fn.apply(context, args); }, delay); }; }
component.html