我知道Vue.js内置了在输入字段上去抖的功能。我已经创建了一个滑块来触发一个不使用输入字段的方法,我想知道我是否可以利用方法中的去抖功能。
除了简单地向输入添加去抖动之外,甚至可以使用此功能吗?或者我需要为此编写自己的功能吗?
我刚刚尝试过这样的事情,但它似乎不起作用:
this.$options.filters.debounce(this.search(), 2000);
答案 0 :(得分:13)
对于想知道如何做到这一点的人。我通过使用我发现的一个很棒的小片段来解决这个问题:
我的数据中的属性
timer: 0
去抖动功能
// clears the timer on a call so there is always x seconds in between calls
clearTimeout(this.timer);
// if the timer resets before it hits 150ms it will not run
this.timer = setTimeout(function(){
this.search()
}.bind(this), 150);
答案 1 :(得分:4)
你将this.search()执行结果放入debounce中,试试这个:
var bufferSearch = Vue.options.filters.debounce(this.search.bind(this), 150);
bufferSearch();