7

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);
1
  • 2
    Doesn't look like there's any easy-to-call debounce function in vue.js - just write one - it's about 5 lines of code and you should be able to find numerous implementations on SO answers or a quick google. Commented Jun 2, 2016 at 9:35

2 Answers 2

17

For anyone who is wondering on how to do this. I fixed this by using an awesome little snippet I found:

Attribute in my data

timer: 0

Debounce functionality

// 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);
Sign up to request clarification or add additional context in comments.

4 Comments

Man!! This was so helpful after like 2 hours dealing this $hit. Love you man.
I'm not sure if this is where you got it from, but I read a similar snippet here: schier.co/blog/2014/12/08/… It might be worth giving credit to the author, if that was your source.
@bjmc got it from a codepen of someone else, this is not the source.
Would be good if you credited the source. e.g. the codepen URL
4

You are put this.search() execution result into debounce, try this:

var bufferSearch = Vue.options.filters.debounce(this.search.bind(this), 150);
bufferSearch();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.