Vue去抖动的方法? [英] Vue debounce a method?

查看:27
本文介绍了Vue去抖动的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 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去抖动的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆