问题描述
我知道Vue.js内置了在输入字段上去抖的功能。我创建了一个滑块,它触发了一个不使用输入字段的方法,我想知道我是否可以利用方法中的去抖功能。
I know Vue.js has functionality built in to debounce on an input field. I have created a slider that fires a method that does not use an input field though, and I was wondering if I can take advantage of the debounce functionality inside of a method.
除了简单地向输入添加去抖动之外,甚至可以使用此功能吗?或者我是否需要为此编写自己的功能?
Is it even possible to use this functionality outside of simply adding a debounce to an input? Or do I need to write my own functionality for this?
我刚尝试过这样的事情,但它似乎不起作用:
I've just tried doing something like this but it does not seem to work:
this.$options.filters.debounce(this.search(), 2000);
推荐答案
对于想知道如何做的人。我通过使用我发现的一个很棒的小片段解决了这个问题:
For anyone who is wondering on how to do this. I fixed this by using an awesome little snippet I found:
我数据中的属性
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);
这篇关于Vue辩解一种方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!